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

发布于 2024-11-09 00:00:44 字数 1024 浏览 5 评论 0原文

我对一个看似重复的问题表示歉意,但我看过的几十个问题实际上都没有遇到相同的问题。

我有以下目录结构:

/.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹泡泡o 2024-11-16 00:00:44

让你的根.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]

对于这个简单的要求,不需要在管理文件夹中拥有.htaccess。

Have your root .htaccess 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 admin folder for this simple requirement.

埋葬我深情 2024-11-16 00:00:44

根文件夹 .htaccess: 的这一行

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

导致对不存在的文件路径的所有请求都被重定向到根文件夹的 index.php。这就是问题所在。
一种可能的解决方案是将上面的行替换为以下几行:

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

通过添加 L (最后一个)标志并按此顺序编写规则,您将使 Apache 正确重定向您的请求,并且无需将指令重写到 /subfolder/.htaccess 中。

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文