1

I have a php site. For all of the page links I use foo.htm, and internally rewrite this to foo.php with .htaccess:

RewriteRule ^(.*)\.htm$ $1.php [NC,L]

This works great - however, it still allows you to use the foo.php url. I would like to 301 redirect foo.php to foo.htm to prevent any old foo.php search engine results from hanging around and rewrite the foo.htm url internally to foo.php

I can't figure out how to do this without creating a loop.

2 Answers2

0
RewriteRule ^(.*)\.htm$ $1.php [NC,L]
RewriteRule ^(.*)\.php$ $1.htm [NC,R]

the option L stops the rewriting engine, preventing a loop.

τεκ
  • 2,994
  • 1
  • 16
  • 14
0

Put this code in your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{THE_REQUEST} ^GET\s(.+)\.php [NC]
RewriteRule ^ %1.htm [R=301,L]

RewriteRule ^(.*)\.htm$ /$1.php [L,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643