1

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?

  • I assume /proj must be your document root? Is /area the equivalent of example in your "skimmed structure"? "But going to /example/pages/ in my browser shows the index.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 called pages within the pages directory"? Are pages and pages actually two different strings like pages and foo? – MrWhite Oct 22 at 22:25
  • "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?" - 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 to index.php? – MrWhite Oct 22 at 22:54
  • Ah... "it should be trying to look for a folder called pages within the pages 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 the example directory, not outside of it? – MrWhite Oct 22 at 22:59
  • 1
    @MrWhite hey :) indeed, proj = document root, example = area :) and oh sorry ,the .htaccess is inside the example folder! My bad, I can see how that can be misconstrued lemme amend – treyBake Oct 23 at 8:16
  • 1
    @MrWhite essentially, I have a project that currently routes everything to the /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 like http://localhost/area/somethingelse.php instead of http://localhost/area/pages/somethingelse.php – treyBake Oct 23 at 8:17
2

You need the .htaccess file in the document root in order to rewrite a request of the form /area/somethingelse.php to the /pages subdirectory in the root (ie. /pages/area/somethingelse.php) before the .htaccess file in the subdirectory (ie. /pages/area/.htaccess) is able to rewrite the request a second time to /pages/area/pages/somethingelse.php - the eventual target.

I have a project that currently routes everything to the /pages directory.

However, if this is an entirely separate "project" then you have to be careful of conflicts. Your directives are also rewriting to an index.php file - but you make no reference to this in your question? For the sake of simplicity, I'm going to ignore that for now(!) and simply rewrite to the /pages (and /pages/example/pages) subdirectories.

Try something like the following instead:

/.htaccess (file in the document root)

RewriteEngine On

# Do not process rewritten requests OR requests that map to existing files
RewriteCond %{ENV:REDIRECT_STATUS} . [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Rewrite EVERYTHING to the /pages subdirectory
RewriteRule ^ /pages%{REQUEST_URI} [L]

/pages/example/.htaccess (file in the subdirectory)

RewriteEngine On

# Do not process requests that map to existing files
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Rewrite EVERYTHING to the "pages" sub-subdirectory if not already
# eg. /pages/example/foo.php to /pages/example/pages/foo.php  
RewriteRule ^(?!pages/)(.*) pages/$1 [L]

(?!pages/) is a negative lookahead to avoid rewriting requests that are already for the /pages sub-subdirectory (inside the /pages/example directory).

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.

A request for /example/pages/ will now result in a 403 Forbidden as it will attempt to list the directory contents /pages/example/pages/. This is assuming Options -Indexes is set and you have no index.php file located at /pages/example/pages/index.php.

  • 1
    you kids and your htaccess wizardry haha thank you, works perfectly :) – treyBake Oct 24 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.