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?
/proj
must be your document root? Is/area
the equivalent ofexample
in your "skimmed structure"? "But going to/example/pages/
in my browser shows theindex.php
" - Yes, your root.htaccess
file rewrites the request to/pages/example/pages/index.php
and the.htaccess
file in the subdirectory does nothing since this file exists. I'm struggling to understand why (you think) a request for/example/pages
should "look for a folder calledpages
within thepages
directory"? Arepages
andpages
actually two different strings likepages
andfoo
?http://localhost/area/pages/
intohttp://localhost/area/
using the.htaccess
file in the subdirectories under the top-levelpages
directory?" - 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
?pages
within thepages
directory" - you are expecting the last rule in the subdirectory.htaccess
file 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".htaccess
file be inside theexample
directory, not outside of it?/pages
directory. 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.php
instead ofhttp://localhost/area/pages/somethingelse.php