I am trying to remove file extensions and trailing slashes from the URI's I'm using as I rebuild my site. I realize that removing the trailing slashes from URLs which actually rely on folders is ridiculous, but I need to preserve the existing URL structure that was created when the site was built in a home made CMS years ago.
I've read through the following and many more.
Remove file extension with .htaccess: Error with trailing slash
Remove .php extension with .htaccess
It seems that people like to answer the first part of the question (re. removing PHP) over and over and skip the 2nd part (also removing the trailing slash).
I am able to remove PHP (or HTML for that matter) file extensions, but am unable to remove both the PHP file extensions and the trailing slashes for directories.
I am using MAMP and working on a mac with OS X El Capitan v. 10.11.6.
Here are the three URIs I need in order to confirm that this is working:
- /localhost/
- /localhost/about
- /localhost/about/guarantee
First, I tried to make /localhost/ and /localhost/about work.
In my root directory, I have the following files:
- index.php
- about.php
I have this in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
When I browse to /localhost/, I get the home page.
When I browse to /localhost/about, I get the desired result - the about page with no file extension or trailing slash.
I.e., it works so far.
In order to make /localhost/about/guarantee work, I created a subfolder in the root:
- index.php
- about.php
- /about/guarantee.php
Unfortunately, when I try to visit /localhost/about now, the browser redirects to /about/ and shows:
Index of /about
Parent Directory
guarantee.php
I.e., I can no longer get to /localhost/about.
I can get to /localhost/about/guarantee though.
So, I try rearranging the files. I add an index.php file to the /localhost/about/ folder:
- index.php
- about.php
- /about/index.php
- /about/guarantee.php
I am now able to visit /localhost/ and /localhost/about/ (WITH THE TRAILING SLASH) and /localhost/about/guarantee.
I cannot find a solution that will remove the trailing slash from /localhost/about/.
I would love some help with my .htaccess file.