创建 Apache mod 重写规则来处理对子目录的所有请求
我想将所有传入请求重定向到子目录,但不知道如何操作。我查看了此处,但这对我不起作用。
因此,如果有人访问 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以实际上你要求的不是重定向。
重定向意味着Web服务器将发送一个包含子目录的新URL,然后浏览器将请求这个新URL,并将其显示在浏览器的URL栏中。
您想要的是将目录结构透明地映射到不同的真实目录结构。这就是 Apache 使用
Alias
指令可以轻松完成的事情。 Mod-rewrite 也可以做到这一点,因为 RewriteRules 不包含[R]
标记,这意味着内部重写,而不是重定向 HTTP 响应。[QSA]
标签还会告诉 mod-rewrite 将当前查询字符串参数保留在重写的 URL 上。但是 Mod-rewrite 并不总是容易理解,并且您的问题对于基本的Alias
,它几乎总是包含在 Apache 中(比 mod-rewrite 更常见)。这应该没问题:
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 basicAlias
, which is almost always included in Apache (more often than mod-rewrite).this should be ok:
尝试将以下内容添加到站点根目录中的 .htaccess 文件中。
默认情况下会传递查询字符串参数。
Try adding the following to the .htaccess file in the root directory of your site.
Query string parameters are passed through by default.