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?
I’m not sure what the benefit of doing this is, assuming you only have your link URLs pointing to the
.htmlpaths, but that aside, you’re correct that the rewritten URL is fed back tomod_rewrite. This will always happen when using it in a per-directory (.htaccess) context, becausemod_rewritehas 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: