0

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.

Community
  • 1
  • 1
C. J.
  • 11
  • 2
  • A simple google search would have provided an answer – RiggsFolly Feb 01 '17 at 01:41
  • It would seem. Questions about removing php, asp, html etc file extensions are all over the place and on Stack. But people always post the same answers - showing several ways to remove file extensions OR add or remove trailing slashes. I can't find a solution to my particular puzzle. – C. J. Feb 01 '17 at 04:38
  • BTW RiggsFolly, you marked this a duplicate to a question I already cited in my post as not answering the question. See "I've read through the following and many more. Remove file extension with .htaccess: Error with trailing slash Remove .php extension with .htaccess" – C. J. Feb 01 '17 at 04:43
  • This will help clarify it I think. I've tried making /localhost/about work by putting a php file at /localhost/about.php and by putting a php file at /localhost/about/index.php. The first one makes /localhost/about work without a trailing slash, but only if there is NO /about/ folder, so I can't then make /localhost/about/guarantee work. If I create an /about/ folder, the server ignores the /about.php file and looks for an index file in that folder. So, to make /localhost/about/guarantee work, I cannot have /localhost/about working. – C. J. Feb 01 '17 at 05:11

0 Answers0