I have a project with this skimmed structure:
-proj
-----pages
---------example
-------------pages
-------------.htaccess
---------.htaccess
I managed to get it working so that all url's go to /pages from the get-go using my project root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} . [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite /pages/area/index.php url -> /area/
RewriteCond %{REQUEST_URI} /(.+)
RewriteRule !\.[a-z0-4]{2,4}$ /pages/%1index.php [NC,L]
# remaining req's get rewritten to /pages/ -> now we don't need index.php in tree root
RewriteRule (.*) /pages/$1 [L]
This code was taken from an answer elsewhere and as I'm no htaccess guru, I thought I could use it in my subdirectory .htaccess file:
RewriteEngine Off
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} . [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite /pages/area/index.php url -> /area/
RewriteCond %{REQUEST_URI} /(.+)
RewriteRule example/pages/\.[a-z0-4]{2,4}/$ /example/pages/%1index.php [NC,L]
# remaining req's get rewritten to /pages/ -> now we don't need index.php in tree root
RewriteRule ^example/(.*) /example/pages/$1 [L]
But going to /example/pages/ in my browser shows the index.php instead of 404'ing as it should be trying to look for a folder called pages within the pages directory - but it seems no.
I could try and add this all to the project root .htaccess and it would be a lot simpler to achieve - however, I'm aiming for my project design to be modular, so these example area's can be added/removed whenever.
How do I rewrite http://localhost/area/pages/ into http://localhost/area/ using the .htaccess file in the subdirectories under the top-level pages directory?
/projmust be your document root? Is/areathe equivalent ofexamplein your "skimmed structure"? "But going to/example/pages/in my browser shows theindex.php" - Yes, your root.htaccessfile rewrites the request to/pages/example/pages/index.phpand the.htaccessfile in the subdirectory does nothing since this file exists. I'm struggling to understand why (you think) a request for/example/pagesshould "look for a folder calledpageswithin thepagesdirectory"? Arepagesandpagesactually two different strings likepagesandfoo?http://localhost/area/pages/intohttp://localhost/area/using the.htaccessfile in the subdirectories under the top-levelpagesdirectory?" - That doesn't seem to make sense from the information given? Do you have a functioning website in the root, as well as in the named subdirectories? What is the URL structure you are striving for? And where are these URLs intended to be routed to? Are you routing everything toindex.php?pageswithin thepagesdirectory" - you are expecting the last rule in the subdirectory.htaccessfile to "win". No, it doesn't work like that. "I'm aiming for my project design to be modular, so these example area's can be added/removed whenever." - then shouldn't the "subdirectory".htaccessfile be inside theexampledirectory, not outside of it?/pagesdirectory. However, I'd like a pages subdirectory inside each area for, well their other pages. But I don't want pages in the area url, so I'd likehttp://localhost/area/somethingelse.phpinstead ofhttp://localhost/area/pages/somethingelse.php