简体   繁体   English

.htaccess 在根和子文件夹中,每个都重定向到自己的 index.php

[英].htaccess in root and subfolder, each to redirect to own index.php

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我希望所有页面请求都由/index.php处理,除非请求开始/subfolder在这种情况下它应该由/subfolder/index.php处理

  • eg /abc to be rewritten to /index.php?u=abc例如/abc被重写为/index.php?u=abc
  • eg /subfolder/def to be rewritten to /subfolder/index.php?u=def例如/subfolder/def被重写为/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.子文件夹中的请求由根index.php处理,而不是子文件夹之一。 (Except requests for /subfolder ) (除了对/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]

Have your root .htaccess like this:让你的根 .htaccess 像这样:

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 admin folder for this simple requirement.对于这个简单的要求,不需要在 admin 文件夹中有 .htaccess。

This line of the root folder .htaccess:根文件夹 .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 .导致对不存在的文件路径的所有请求都被重定向到根文件夹的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 .通过添加L (最后一个)标志并按此顺序编写规则,您将让 Apache 正确重定向您的请求,并消除将指令重写到/subfolder/.htaccess的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM