.htaccess in the root and subfolder, each of which is redirected to its own index.php

I apologize for the seemingly duplicate question, but none of the dozens that I looked at actually had the same problem.

I have the following directory structure:

/.htaccess /index.php /subfolder/.htaccess /subfolder/index.php 

I would like all page requests to be processed using /index.php if the request does not start /subfolder , in which case it should be processed /subfolder/index.php

  • eg. /abc , which will be rewritten to /index.php?u=abc
  • eg. /subfolder/def to overwrite /subfolder/index.php?u=def

I spun around, so any help would be greatly appreciated.

EDIT: forgot to mention the problem! Requests inside the subfolder are handled by the index.php root, not the subfolder. (Excluding queries for /subfolder )

Current file contents

/.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] 
+6
source share
2 answers

Your .htaccess root is like this:

 Options -Indexes -MultiViews +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(?!admin/)(.+)$ /index.php?u=$1 [NC,QSA,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^admin/(.+)$ /admin/index.php?u=$1 [NC,QSA,L] 

There is no need to have .htaccess in the admin folder for this simple requirement.

+9
source

This line of the root folder .htaccess:

 RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA] 

causes redirection of all requests to nonexistent paths to the root index.php folder. This is problem. One possible solution would be to replace the above line with the following pairs:

 RewriteRule ^subfolder/(.*)$ /subfolder/index.php?u=$1 [L,NC,QSA] RewriteRule ^(.+)$ /index.php?u=$1 [L,NC,QSA] 

By adding the L flag (the last one) and writing down the rules in this order, you will get Apache to properly redirect your requests and eliminate the need to rewrite directives in /subfolder/.htaccess .

+1
source

Source: https://habr.com/ru/post/888822/


All Articles