Welcome to WebmasterWorld Guest from 54.226.179.247

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Dynamic File Extension with mod rewrite

     
2:30 am on Jun 12, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 30, 2004
posts:196
votes: 0


I have a client with a demo app built in php, but they want the browser to show .do extensions instead of .php extensions. I've tried numerous .htaccess permutations, and here is what isn't working:

Options +FollowSymlinks
RewriteEngine On

#if file extension is .do, process as .php page on server
RewriteRule ^(.*)\.do$ $1.php [nc]

#if file extension is .php, redirect to .do url
RewriteRule ^(.*)\.php$ $1.do [R=301,L]

The first bit by itself works fine, if you enter blah/blah.do?id=2, server will execute blah/blah.php?id=2

However if I add the second rule (to redirect .php requests to .do urls) it all breaks down.

I've been struggling with this for a few hours and my brain is starting to melt down. Any help would be appreciated, thank you!

1:42 pm on June 12, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Yes, the problem is that the two rules always countermand each other, leading to an 'infinite' rewrite-redirect loop. You must check the original client request before redirecting in order to prevent this:

# if requested file extension is .do, rewrite to .php file on server
RewriteRule ^(.*)\.do$ $1.php [L]
#
# if file extension .php is directly-requested by client, redirect to .do url
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1.do [R=301,L]

If you want to handle uppercase/mixed-case variants of ".do" then do it separately, redirecting uppercase/mixed-case variations to the all-lowercase URL; Otherwise, you risk duplicate-content problems.

Jim

3:38 pm on June 12, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 30, 2004
posts:196
votes: 0


Jim,

Thank you very much, it worked a treat! In order to "learn how to fish" I'll be spending some time studying the example you gave.
I see on the first line you added the [L] parameter, which I think means that should be the last rule if true.
And the new RewriteCond, does that check to make sure that the client requested it and not a server redirect?

Anyway thanks again!

11:11 am on June 13, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


[L] means stop here. This is correct.

Yes. It tests THE_REQUEST - what the browser originally requested.

Personally, I always put the redirects first in the code and the rewrites last too.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members