双斜杠apache配置

发布于 2024-08-25 01:29:25 字数 640 浏览 4 评论 0原文

我正在部署一个 ror 应用程序,现在我必须重写 url (在 apache 中)以

  • 在 url 中添加前缀 www 在 url 中
  • 添加 / 到 url 的末尾

所以我采用了以下方法:

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com/$1 [R=301,L]

问题是它附加了两个我的网址的尾部斜杠 因此,例如资源/问题/询问正在变成:

http://foo.com//question/ask

我尝试在所有重写规则之前添加以下规则以尝试删除双//:

RewriteCond %{REQUEST_URI} ^//
RewriteRule ([^/]*)/+(.*) http://www.foo.com/$1/$2 [R=301,L]

但它不起作用..有任何想法可以撕掉所有额外的“//”添加到网址?

i'm deploying a ror application and now i have to rewrite the url (in apache) to

  • add a prefix www to the url
  • add / to the end of the url

So i took the following approach:

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com/$1 [R=301,L]

The problem is that it is appending two trailing slash to my url
So for example a resource /question/ask are becoming:

http://foo.com//question/ask

I tried to add the following Rule before all my Rewrite rules to try to remove the double //:

RewriteCond %{REQUEST_URI} ^//
RewriteRule ([^/]*)/+(.*) http://www.foo.com/$1/$2 [R=301,L]

but it didnt work.. any idea to rip off all extras "//" added to the url?

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

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

发布评论

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

评论(1

累赘 2024-09-01 01:29:25

$1 将在开头包含一个 /。你可能想要

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com$1 [R=301,L]

The $1 will include a / at the beginning. You probably want

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com$1 [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文