根目录和子文件夹中的.htaccess,每个都重定向到自己的index.php
我对一个看似重复的问题表示歉意,但我看过的几十个问题实际上都没有遇到相同的问题。
我有以下目录结构:
/.htaccess
/index.php
/subfolder/.htaccess
/subfolder/index.php
我希望所有页面请求都由/index.php
,除非请求开始 /subfolder
在这种情况下,它应该由 /subfolder/index.php
处理,
- 例如
/abc
被重写为/index.php?u=abc
- 例如
/subfolder/def
被重写为/subfolder/index.php ?u=def
我一直在绕这个问题,所以任何帮助将非常感激。
编辑:忘记提及这个问题! 子文件夹内的请求由根 index.php
处理,而不是由子文件夹处理。 (/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]
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
- e.g.
/abc
to be rewritten to/index.php?u=abc
- e.g.
/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
)
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让你的根.htaccess像这样:
对于这个简单的要求,不需要在管理文件夹中拥有.htaccess。
Have your root .htaccess like this:
There is no need to have .htaccess in admin folder for this simple requirement.
根文件夹 .htaccess: 的这一行
导致对不存在的文件路径的所有请求都被重定向到根文件夹的
index.php
。这就是问题所在。一种可能的解决方案是将上面的行替换为以下几行:
通过添加
L
(最后一个)标志并按此顺序编写规则,您将使 Apache 正确重定向您的请求,并且无需将指令重写到/subfolder/.htaccess
中。This line of the root folder .htaccess:
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:
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
.