I have all site pages in a subdirectory like this...
http://www.example.com/pages/myfile.php
I want the URL to look like this...
http://www.example.com/myfile
Where both the subdirectory called pages
and the .php
file extension are removed from the URL.
My latest (partial) attempt...
Options All -Indexes +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/pages%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/pages%{REQUEST_URI} -d
RewriteRule ^(.*)$ /pages/$1.php [NC,L]
However, this totally breaks DirectoryIndex
. When I go to http://www.example.com/
or http://www.example.com/foo/
, I get a 404 error instead of defaulting to index.php
as defined by DirectoryIndex
.
Apparently, it treats everything as a file name instead of recognizing the lack of a file name (directory) and attempting to use index.php
.
I tried incorporating this solution into mine, it fixed the DirectoryIndex
issue, but it broke everything else.
Is there a solution? Please include a detailed explanation within your answer so I can learn where/how I was going wrong.