I am new to mod_rewrite and searched for hours to try and find a simple solution to remove .html and .php file extensions.
I thought I had found the solution below:
- Code: Select all
Options +FollowSymLinks
RewriteEngine On
# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,QSA]
# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
On the most part it works perfectly but the problem is if you have a folder and file name at yoursitename.com/folder you just get sent to the index of that folder.
How would I force the mod_rewrite to point to the .html file "folder" instead of the folder named "folder"?!
Also do I need to add a trailing slash "/" onto the url or is the removal of trailing slash code above fine? I would need the code for addition of trailing slash if this is the case.
Thanks in advance