mod_rewrite、尾部斜杠删除和当前目录

发布于 2024-11-07 17:47:35 字数 743 浏览 4 评论 0原文

我已经仔细阅读了相关问题,但我无法弄清楚这个小警告。我见过的通过 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 技术交流群。

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

发布评论

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

评论(1

裂开嘴轻声笑有多痛 2024-11-14 17:47:35

好吧,我解决了附加问题:

Options +FollowSymLinks

RewriteEngine On
RewriteBase /_dev/_projects/mf_frame

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) ?routePath=$1 [L,QSA]

我缺少 RewriteBase 指令。我会再去脱一次衣服。如果有什么可以做得更好的地方,请随时提出我的方法的替代方案。

Well, I solved it for appending:

Options +FollowSymLinks

RewriteEngine On
RewriteBase /_dev/_projects/mf_frame

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) ?routePath=$1 [L,QSA]

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.

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