重定向到子文件夹时始终保留 URL

发布于 2024-09-10 11:47:14 字数 972 浏览 1 评论 0原文

我想将所有请求重定向到服务器上的某个路径 (/app) 到 /app/app_site 的子目录。以下重写规则可以完成诸如“http://localhost/app/somepage.htm”之类的请求:

RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app(.*)     /app/app_site$1 [L,PT]

这会生成正确的页面,同时保留 URL。此外,“http://localhost/app/”将在 /app/app_site/index.html 处获取索引页面。 html,同时保留 URL 'http://localhost/app/'。

但是,当我输入 'http://localhost/app' 时,会发生以下情况:

我已经快到了,但希望在所有情况下都保留 URL(也包括那些没有尾部斜杠的情况)。有人知道如何做到这一点吗?谢谢!

I want to redirect all requests to a certain path on my server (/app) to a subdirectory at /app/app_site. Following rewrite rules do the job for requests like 'http://localhost/app/somepage.htm':

RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app(.*)     /app/app_site$1 [L,PT]

This results in the correct page, while preserving the URL. Also, 'http://localhost/app/' will fetch the index page at /app/app_site/index.html, while preserving the URL 'http://localhost/app/'.

However, when I enter 'http://localhost/app', following things happen:

I'm nearly there, but would like to preserve the URL in all cases (also those without trailing slash). Anyone have a clue how to do this? Thanks!

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

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

发布评论

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

评论(1

忆伤 2024-09-17 11:47:14

这是 DirectorySlash 已启用,因为您已重写到缺少尾部斜杠的目录,并且在您使用 mod_rewrite 重写 URL 后,mod_dir 会执行此清理操作。

最简单的解决方案是重写 URL,使其始终至少与以斜杠结尾的目录路径匹配,如下所示:

RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app/?(.*)$     /app/app_site/$1 [L,PT]

这可以防止 mod_dir 必须添加尾部斜杠,从而避免外部重定向到您现在正在体验的/app/app_site/

This is the expected behaviour with DirectorySlash enabled, because you've rewritten to a directory that lacks a trailing slash, and mod_dir performs this cleanup after you've rewritten the URL with mod_rewrite.

The easiest solution is to rewrite the URL so that it always at least matches the slash-terminated directory path, like so:

RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app/?(.*)$     /app/app_site/$1 [L,PT]

This prevents mod_dir from having to add the trailing slash, and therefore avoids the external redirection to /app/app_site/ you're experiencing now.

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