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]

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.