重定向到子文件夹时始终保留 URL
我想将所有请求重定向到服务器上的某个路径 (/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' 时,会发生以下情况:
- 在以下位置获取正确的页面: /app/app_site/index.html
- 然而,URL 被重定向到 'http://localhost/app/app_site/< /a>'
我已经快到了,但希望在所有情况下都保留 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:
- the correct page is fetched, at /app/app_site/index.html
- yet, the URL is redirected to 'http://localhost/app/app_site/'
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是
DirectorySlash
已启用,因为您已重写到缺少尾部斜杠的目录,并且在您使用mod_rewrite
重写 URL 后,mod_dir
会执行此清理操作。最简单的解决方案是重写 URL,使其始终至少与以斜杠结尾的目录路径匹配,如下所示:
这可以防止
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, andmod_dir
performs this cleanup after you've rewritten the URL withmod_rewrite
.The easiest solution is to rewrite the URL so that it always at least matches the slash-terminated directory path, like so:
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.