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,061
  • 1
  • 7
  • 15
Sebastian
  • 21
  • 4

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,061
  • 1
  • 7
  • 15
  • Thanks! But now I get an Error 310 (net::ERR_TOO_MANY_REDIRECTS) in Chrome... – Sebastian Dec 28 '11 at 14:56
  • @Sebastian - You must have some other existing rules that are interfering with this, then. I tested this using curl, and now just tested it in Chrome as well, and it is working as expected without any redirect loops. – ziesemer Dec 28 '11 at 15:04
  • This will be very hard to do with a .htaccess file. Every matching rule causes an internal redirect. So what happens is that pagename gets rewritten to pagename.php, which in the next subrequest gets rewritten to pagename, which then in a subrequest etc... – Krist van Besien Apr 22 '13 at 12:06