3

Currently I'm rewriting all incoming requests for *.html to *.php in my .htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteRule ^(.*).html$ $1.php [QSA]

ErrorDocument 404 /404.html

So /something.html is rewritten to /something.php.

However, /something.php is still directly accessible in the browser. Now I want it to redirect to /something.html when people are accessing it in the browser, so as to avoid 2 distinct URLs for the same page of content.

Is this possible to do in my .htaccess? How? I tried R=301 but it's always a redirect loop or something. Any help would be appreciated. Thanks!

1
  • Can you please specify The meaning of => [R=301,L] ? Commented Jul 9, 2014 at 12:29

1 Answer 1

8

Add this above your RewriteRule ^(.*).html$ $1.php [QSA] rule:

RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [L,R=301]

That will redirect the browser/client to the same request but with a .html instead of a .php.

2
  • Can you please specify The meaning of => [L,R=301] ? Commented Jul 9, 2014 at 12:28
  • 1
    @PratikJoshi They are Rewrite flags, the "L" means "Last" which stops rewriting for the current iteration, and the "R" means "Redirect" and the 301 is the redirect code (permanent).
    – Jon Lin
    Commented Jul 9, 2014 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.