使用 .htaccess 替换 URL 中的字符时出现问题

发布于 2024-09-17 19:42:47 字数 591 浏览 0 评论 0原文

我已经尝试了数十种不同的方法来做到这一点,但无法让其中任何一种发挥作用。我的 .htaccess 做了一些事情,比如设置自定义 404 和阻止图像热链接。我想在 URL 上做两件事:添加 www.如果它不存在(相当烦人的 Facebook 登录无法应对两个不同的来源!),并将 // 替换为 / except 在 http: 之后。

我尝试过这个:

# Replace // with /
RewriteCond %{REQUEST_URI}     (.*)(?<!http:)\/{2,5}(.*)
RewriteRule .*                 %1/%2 [R=301,L]

还有这个:

# Replace // with /
RewriteCond %{REQUEST_URI}     (.*).com\/\/(.*)
RewriteRule .*                 %1.com/%2 [R=301,L]

以及各种排列。有人能告诉我我做错了什么吗?

我需要这样做,因为有时会在 .com 和 URL 的其余部分之间插入多个 / 。

谢谢

I've tried dozens of different ways of doing this but can't get any of them to work. My .htaccess does a few things, like setting a custom 404 and blocking image hotlinking. I want to do two things on the URL: add www. if it isn't there (rather annoying Facebook login can't cope with two different sources!), and replacing // with / except after http:.

I've tried this:

# Replace // with /
RewriteCond %{REQUEST_URI}     (.*)(?<!http:)\/{2,5}(.*)
RewriteRule .*                 %1/%2 [R=301,L]

And this:

# Replace // with /
RewriteCond %{REQUEST_URI}     (.*).com\/\/(.*)
RewriteRule .*                 %1.com/%2 [R=301,L]

And all sorts of permutations. Can anybody tell me what I'm doing wrong?

I need to do this because sometimes multiple /s are being inserted between the .com and the rest of the URL.

Thanks

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

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

发布评论

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

评论(3

动次打次papapa 2024-09-24 19:42:47

我认为 http:// 根本不是 REQUEST_URI 的一部分(或任何其他环境变量的一部分)。早在发出实际请求之前,它就会被浏览器解析出来,并用于确定请求的性质。

我可能是错的,但我认为这在 htaccess 级别上是无法修复的。首先必须正确设置链接的格式。

更新:查看 Apache 传递给 PHP 的信息,我认为我是对的。用于发出请求的协议不是我们要使用的 URI 组件的一部分。

I don't think http:// is part of REQUEST_URI at all (or of any other environment variable for that matter). It will get parsed out by the browser, and used to determine the nature of the request, long before the actual request is made.

I can be wrong, but I think this is not fixable on htaccess level. The link would have to be properly formatted in the first place.

Update: Looking at the information Apache passes on to PHP, I think I'm right. The protocol used to make the request is not part of the URI components we get to play with.

帅气尐潴 2024-09-24 19:42:47

以下是如何强制 www.

<IfModule mod_rewrite.c>
#Add WWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#End Add WWW
</IfModule>

考虑到 @Tim 下面提到的内容,我会检查 %{REQUEST_URI} 是否包含 //,并且将是我的RewriteCond

<IfModule mod_rewrite.c>
#Replace // with /
RewriteCond %{REQUEST_URI} // [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#End Replace // with /
</IfModule>

Here's how to force www.:

<IfModule mod_rewrite.c>
#Add WWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#End Add WWW
</IfModule>

Considering what @Tim mentioned below, I would check %{REQUEST_URI} if it contains //, and that would be my RewriteCond:

<IfModule mod_rewrite.c>
#Replace // with /
RewriteCond %{REQUEST_URI} // [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#End Replace // with /
</IfModule>
扛刀软妹 2024-09-24 19:42:47

我不确定为什么您会遇到多个斜杠的问题,因为它应该能够以任何一种方式解析文件。但是,可以通过重定向来检查和删除它们(我已将其与您的force-www 结合起来,因此最多有一个外部重定向):

RewriteCond %{THE_REQUEST} ^[A-Z]+\s[^\s]*/{2,} [OR]
RewriteCond %{HTTP_HOST}  !^www\.
RewriteCond %{HTTP_HOST}   ^(www\.)?(.*)$
RewriteRule ^ http://www.%2%{REQUEST_URI} [R=301,L]

请注意 %{REQUEST_URI} 删除了重复的斜杠(仅在 mod_rewrite 中,这对于稍后的脚本而言并非如此),因此我们可以在重定向中使用它来自动为我们处理该问题。不过,原始请求仍将包含多个斜杠,因此我们通过检查 %{THE_REQUEST} 来检查它们。

I'm not sure why you're experiencing trouble with the multiple slashes, since it should be able to resolve the file either way. However, it is possible to check for and remove them with a redirect (I've combined this with your force-www so there's at most one external redirection):

RewriteCond %{THE_REQUEST} ^[A-Z]+\s[^\s]*/{2,} [OR]
RewriteCond %{HTTP_HOST}  !^www\.
RewriteCond %{HTTP_HOST}   ^(www\.)?(.*)$
RewriteRule ^ http://www.%2%{REQUEST_URI} [R=301,L]

Note that %{REQUEST_URI} has the duplicate slashes removed (only in mod_rewrite, this isn't true for scripts later on), so we can use it in the redirect to automatically take care of that issue for us. The original request will still have the multiple slashes though, so we check for them by examining %{THE_REQUEST}.

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