Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

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 has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

1 Answer

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.

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
add comment

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