apache - Mod rewrite to remove subdirectory and file extension without breaking DirectoryIndex -


i have site pages in subdirectory this...

http://www.example.com/pages/myfile.php 

i want url this...

http://www.example.com/myfile 

where both subdirectory called pages , .php file extension removed url.

my latest (partial) attempt...

options -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, totally breaks directoryindex. when go http://www.example.com/ or http://www.example.com/foo/, 404 error instead of defaulting index.php defined directoryindex.

apparently, treats file name instead of recognizing lack of file name (directory) , attempting use index.php.

i tried incorporating this solution mine, fixed directoryindex issue, broke else.

is there solution? please include detailed explanation within answer can learn where/how going wrong.

try in root .htaccess:

options -indexes +followsymlinks directoryindex index.php  rewriteengine on rewritebase /  # add trailing slash if pages/<uri> directory rewritecond %{document_root}/pages/$1 -d rewriterule ^(.*?[^/])$ %{request_uri}/ [l,r=302]  rewriterule ^/?$ pages/index.php [l]  # skip files , directories rewrite rules below rewritecond %{request_filename} -d [or] rewritecond %{request_filename} -f rewriterule ^ - [l]  # if corresponding .php file exists in pages/ directory rewritecond %{document_root}/pages/$1\.php -f rewriterule ^(.+?)/?$ pages/$1.php [l]  # route requests pages/ rewriterule ^((?!pages/).*)$ pages/$1 [l,nc] 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -