views:

38

answers:

1

My intent is to rewrite urls ending in .html to .php (not actually all of them, but that shouldn't really matter with regard to this question). I'd also like to disallow urls ending in .php (so that the user can access every page only using urls with .html extension).

I'm using these rules inside .htaccess:

RewriteRule ^(.*)\.php$ $1.html [R,L]
RewriteRule ^(.*)\.html$ $1.php [L]

However, this causes a redirection loop. I suppose this happens because the rewritten .html to .php url is fed back to mod_rewrite and causes the first rule to trigger.

Can anyone help me?

+1  A: 

I'm not sure what the benefit of doing this is, assuming you only have your link URLs pointing to the .html paths, but that aside, you're correct that the rewritten URL is fed back to mod_rewrite. This will always happen when using it in a per-directory (.htaccess) context, because mod_rewrite has to assign itself as the request handler to work correctly in this stage of the Apache request process chain.

Getting to your actual issue, you can solve the problem by conditioning the first rule based upon the original request sent to the server. This could be done like so:

RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^\s]+)\.php\s
RewriteRule .* %1.html [R=301,L]

RewriteRule ^(.*)\.html$ $1.php
Tim Stone
Thank you very much, that's exactly what I needed. There's actually little benefit in doing this but I am pretty new at Apache and wanted to improve my understanding of mod_rewrite.
Utaal
No problem, glad it works for you. Well, that's a pretty good reason itself. `mod_rewrite` is a tricky beast, sometimes.
Tim Stone