This script works perfect it forces the trailing slash and hides the .php extension

BUT! it does not redirect people going directly to the .php extension.

How can I also force people going directly to the file.php to /file/

RewriteEngine On RewriteRule ^(.*)/$ /$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.)/$ RewriteRule ^(.)$ http://www.mysite.com/$1/ [R=301,L]
7 Years
Discussion Span
Last Post by kylegetson

Hey i really need an answer for this, i thought this would be the best place to post this question !


try this:

# redirect visitors to not have .php in the url
RewriteCond %{REQUEST_FILENAME} -f # only if the file requested actually exists
RewriteCond %{REQUEST_FILENAME} \.php$ # only if the file ends in .php
RewriteRule ^/(.*).php$ /$1/ [R=301,L] # 301 them to the same location, without the .php ending

# serve the .php file, even though its not on the url
RewriteCond %{REQUEST_FILENAME} !-f #only if the file requested DOES NOT actually exists
RewriteRule ^/(.*)$ /$1.php [L] # serve the php file

I did not test this, so test it out before using in a production environment. you may also want to add in a condition with -d for making sure the directory doesnt exist before redirecting or attempting to serve a specific file.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.