2

With htaccess I'm trying to make my sites urls clean. I use very basic urls like: www.mysite.com/pagename.php ("pagename" is variable).

I want www.mysite.com/pagename to display the content of /pagename.php
So this is in my htaccess-file now:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

But I also want my old urls (/pagename.php), when called, to be rewritten to www.mysite.com/pagename

How to do this? I can't figure it out (get loops all the time)...

Thanks in advance!

ziesemer
  • 1,063

1 Answers1

0

Add another block to define the rewrite:

Options +FollowSymlinks
RewriteEngine On

RewriteBase /

RewriteRule ^([^\.]+)\.php$ $1 [NC,R=301,L]

RewriteRule ^([^\.]+)$ $1.php [NC,PT,L]

Note that RewriteBase is very important here. (See the documentation at http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase) - otherwise your URLs will be rewritten to something like <hostname>/var/www/pagename.php instead of the expected <hostname>/index.php.

ziesemer
  • 1,063