I am attempting to create clean Web addresses on my small website. Now I been successful in setting up apache so every request like mysite.com/page works. However I likewise want that demands for mysite.com/page.php is going to be rerouted to mysite.com/page. I have set atmosphere variable inshtaccess to see if i recently been rerouted to avoid loops, however i get them... Here's the .htaccess:

<IfModule mod_rewrite.c>
Options +FollowSymlinks

RewriteEngine On
RewriteBase /

# set the variable
RewriteRule ^$ - [L]
RewriteRule ^(.*)$ $1 [E=REWROTE:0]

# this works fine
# make mysite.com/page work and set REWROTE variable to 1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,E=REWROTE:1]

# but if i enable this, i get redirected and then stuck in a loop
# redirect mysite.com/page.php to mysite.com/page , but only if REWROTE not 1
#RewriteCond %{ENV:REWROTE} !^1$
#RewriteRule ^(.*)\.php$ $1 [R=301,L]


Add this rule above your overall rewrite rules to prevent redirecting when the request was already rerouted once (ref):

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

You can look into the request line for which has initially been asked for:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ (/[^\ ]*)\.php[?\ ]
RewriteRule \.php$ %1 [R=301,L]

Oh, and also the first argument from the [cde] directive isn't a regular expression but simply a string. Therefore the getting away the RewriteCond is wrong.

Can there be some reason you cannot make use of the finish-of-string termination character, $ ?


will redirect /page.php to /page and can not inflict redirecting on /page .

Essentially, using techniques like setting atmosphere variables and adding inspections for REDIRECT_STATUS won't be very robust.