Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

I want 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

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

Thanks in advance!

share|improve this question

marked as duplicate by Jenny D, Ward, Dave M, Falcon Momot, Scott Pack Nov 5 '13 at 20:00

This question was marked as an exact duplicate of an existing question.

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 - otherwise your URLs will be rewritten to something like <hostname>/var/www/pagename.php instead of the expected <hostname>/index.php.

share|improve this answer
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

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