| Dynamic File Extension with mod rewrite
|
ignatz

msg:3672669 | 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!
|
jdMorgan

msg:3673006 | 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. Jim
|
ignatz

msg:3673125 | 3:38 pm on Jun 12, 2008 (gmt 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!
|
g1smd

msg:3673936 | 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.
|
|
|