创建 Apache mod 重写规则来处理对子目录的所有请求

发布于 2024-12-27 06:46:32 字数 472 浏览 2 评论 0原文

我想将所有传入请求重定向到子目录,但不知道如何操作。我查看了此处,但这对我不起作用。

因此,如果有人访问 www.example.com/test,我希望它重定向到:www.example.com/test/subdirectory,但 URL 仍然需要www.example.com/test

我还需要参数才能工作,例如 www.example.com/test/index.php?action=home 必须重定向到www.example.com/test/subdirectory/index.php?action=home

我希望有人能帮助我!

I want to redirect all incoming requests to a subdirectory, but can't figure out how. I looked here but that did not work for me.

So, if someone visits www.example.com/test, I want it to redirect to: www.example.com/test/subdirectory, but the URL still needs to be www.example.com/test

I also need the parameters to work, for example www.example.com/test/index.php?action=home must redirect to www.example.com/test/subdirectory/index.php?action=home.

I hope someone can help me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

从﹋此江山别 2025-01-03 06:46:32

所以实际上你要求的不是重定向。

重定向意味着Web服务器将发送一个包含子目录的新URL,然后浏览器将请求这个新URL,并将其显示在浏览器的URL栏中。

您想要的是将目录结构透明地映射到不同的真实目录结构。这就是 Apache 使用 Alias 指令可以轻松完成的事情。 Mod-rewrite 也可以做到这一点,因为 RewriteRules 不包含 [R] 标记,这意味着内部重写,而不是重定向 HTTP 响应。 [QSA] 标签还会告诉 mod-rewrite 将当前查询字符串参数保留在重写的 URL 上。但是 Mod-rewrite 并不总是容易理解,并且您的问题对于基本的 Alias,它几乎总是包含在 Apache 中(比 mod-rewrite 更常见)。

这应该没问题:

Alias /test /test/subdirectory

So in fact what you ask for is not a redirect.

A redirect means the web server will send a new url containing the subdirectory, the browser will then request this new url, and show it in the url bar of the browser.

What you want is to tranparently map an directory structure to a real directory structure which differs. This is what Apache can do easily with the Alias instruction. Mod-rewrite could also do that, with RewriteRules not containing the [R] tag, this would imply an internal rewrite, not a redirect HTTP response. A [QSA] tag would also tell mod-rewrite to keep the current query string parameters on the rewritten url. But Mod-rewrite is not always simple to understand, and your problem is simple enough for a basic Alias, which is almost always included in Apache (more often than mod-rewrite).

this should be ok:

Alias /test /test/subdirectory
才能让你更想念 2025-01-03 06:46:32

尝试将以下内容添加到站点根目录中的 .htaccess 文件中。
默认情况下会传递查询字符串参数。

RewriteEngine on
RewriteBase /

#if not an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite to subdirectory
RewriteRule ^(.*)(/[^/]+)?$ /$1/subdirectory/$2 [L,QSA]

Try adding the following to the .htaccess file in the root directory of your site.
Query string parameters are passed through by default.

RewriteEngine on
RewriteBase /

#if not an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite to subdirectory
RewriteRule ^(.*)(/[^/]+)?$ /$1/subdirectory/$2 [L,QSA]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文