I apologise for a seemingly duplicate question, but none of the dozens I've looked at actually had the same problem.
I have the following directory structure:
/.htaccess
/index.php
/subfolder/.htaccess
/subfolder/index.php
I'd like all requests for pages to be handled by /index.php
, unless the request starts /subfolder
in which case it should be handled by /subfolder/index.php
/abc
to be rewritten to /index.php?u=abc
/subfolder/def
to be rewritten to /subfolder/index.php?u=def
I've been going round in circles over this, so any help will be massively appreciated.
EDIT: forgot to mention the problem!
Requests within the subfolder are handled by the root index.php
, not the subfolder one. (Except requests for /subfolder
)
/.htaccess
Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA]
/subfolder/.htaccess
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /admin/index.php?u=$1 [NC,QSA]
This line of the root folder .htaccess:
RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA]
is causing all the requests to non-existent filepaths to be redirected to the root folder's index.php
. That's the problem.
One possible solution could be to substitute the above line with this couple of lines:
RewriteRule ^subfolder/(.*)$ /subfolder/index.php?u=$1 [L,NC,QSA]
RewriteRule ^(.+)$ /index.php?u=$1 [L,NC,QSA]
By adding the L
(last) flag and writing the rules in this order you'll get Apache to redirect correctly your requests, and eliminate the need for rewriting directives into /subfolder/.htaccess
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With