Ad

Hard Redirect File.php To /file (when /file Is Already Being Transparently Redirected To /file.php)

- 1 answer

Am already transparently redirecting requests for /file to /file.php -- accessing /file shows /file.php. My .htaccess:

RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-Z]+)/?$ $1.php [L]

Would now like to do a hard redirect for /file.php to /file -- if I hit up /file.php, URL in the address bar must show /file. Tried the following but didn't work:

RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-Z]+)\.php$ $1 [R]
RewriteRule ^([a-zA-Z]+)/?$ $1.php [L]

Any help?

Ad

Answer

You need to perform your rewrite based on the request that the user sent to the server. Something like this should work:

RewriteEngine On

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

RewriteRule ^([a-zA-Z]+)/?$ $1.php [L]
Ad
source: stackoverflow.com
Ad