mod_rewrite、尾部斜杠删除和当前目录
我已经仔细阅读了相关问题,但我无法弄清楚这个小警告。我见过的通过 mod_rewrite 删除尾部斜杠的黄金规则是:
RewriteRule ^(.*)/$ /$1 [R,L]< /代码>。
这一切都很好,但它剥离了前面存在的目录结构。因此,如果我的应用程序引导程序在托管路径的根目录下运行,它就可以工作,但如果在子目录中则不行:
http://localhost/path/to/application/pretty/query/string/
成为
http://localhost/pretty/query/string
(注意;斜杠被删除,但目录也被删除)
如何保留当前目录位置,以便前面的示例返回预期的:
http://localhost/path/to/application/pretty/query/string
更新
归根结底,这是为了保持一致性;附加或删除尾部斜杠都是合适的。我在尝试两种方法之间来回努力,但没有成功。
附加或删除尾部斜杠的答案都是可以接受的!
I've perused the related questions, but I cant figure this little caveat out. The golden rule I've seen tossed around for trailing slash removal via mod_rewrite is:
RewriteRule ^(.*)/$ /$1 [R,L]
.
This is all fine and good, but it strips off the preceding directory structure given one exists. So, if my application bootstrap is running at the root of the hosted path, it works, but not if in a subdirectory:
http://localhost/path/to/application/pretty/query/string/
Becomes
http://localhost/pretty/query/string
(Note; slash is stripped, but so is directory)
How can I preserve the current directory location, so the previous example returns the expected:
http://localhost/path/to/application/pretty/query/string
Update
Ultimately, this is for the sake of consistency; either appending or stripping a trailing slash is a suitable. I'm working back and forth between trying to get either approach to work, with no success.
Answers that either append or strip the trailing slash are acceptable!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我解决了附加问题:
我缺少
RewriteBase
指令。我会再去脱一次衣服。如果有什么可以做得更好的地方,请随时提出我的方法的替代方案。Well, I solved it for appending:
I was missing the
RewriteBase
directive. I'll have another go and stripping. Please feel free to suggest alternatives to my approach if there's something that can be done better.