homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Dynamic File Extension with mod rewrite

10+ Year Member

Msg#: 3672667 posted 2:30 am on Jun 12, 2008 (gmt 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!



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

Msg#: 3672667 posted 1:42 pm on Jun 12, 2008 (gmt 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.



10+ Year Member

Msg#: 3672667 posted 3:38 pm on Jun 12, 2008 (gmt 0)


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!


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

Msg#: 3672667 posted 11:11 am on Jun 13, 2008 (gmt 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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved