规则不将 .co.uk 重定向到 .com

发布于 2024-12-15 04:38:44 字数 475 浏览 0 评论 0原文

我一直在尝试让我的网站在访问者访问 .co.uk 时自动重定向到 .com。如果他们访问domain.com,也会被带到www.domain.com,

我的httpd.conf 中有下面的代码。它似乎可以使用 domain.comwww.domain.com,但不能使用 domain.co.ukwww.domain .co.uk 至 www.domain.com

RewriteEngine on
RewriteBase /      
RewriteCond %{http_host} ^domain.com [NC,OR]
RewriteCond %{http_host} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]

I have been trying to get my site, when a visitor goes to .co.uk to be automatically redirected to .com. As well as if they go to domain.com to be taken to www.domain.com

I have the code below in my httpd.conf. It appears to be working with domain.com to www.domain.com but not domain.co.uk or www.domain.co.uk to www.domain.com

RewriteEngine on
RewriteBase /      
RewriteCond %{http_host} ^domain.com [NC,OR]
RewriteCond %{http_host} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]

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

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

发布评论

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

评论(2

屋檐 2024-12-22 04:38:44

如果您的重写规则位于 htaccess 文件中,那么这应该可以工作。如果它们位于 vhost 文件(或 Apache httpd.conf 本身)中,请尝试删除 /

并尝试始终区分大小写(要习惯,因为大多数语言都区分大小写,这是一个好习惯):


如果在 .htaccess 中:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC,OR]
RewriteCond %{HTTP_HOST} domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]

如果在 vhost 中或 httpd.conf 文件:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC,OR]
RewriteCond %{HTTP_HOST} domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,NC]

请尝试使用 RewriteLog 指令:它可以帮助您追踪此类问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

告诉我它是否有效。

If your rewrite rules are in an htaccess file, this should work. If they are in a vhost file (or the Apache httpd.conf itself) try to remove the /.

And try to always be case sensitive (get used to because most of languages are case sensitive it's a good habit to take):


If in a .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC,OR]
RewriteCond %{HTTP_HOST} domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]

If in a vhost or httpd.conf file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC,OR]
RewriteCond %{HTTP_HOST} domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,NC]

And please try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

Tell me if it works.

遇见了你 2024-12-22 04:38:44

HTTP_HOST 的名称区分大小写吗? RewriteCond 文档始终将其列为 HTTP_HOST,而不是 http_host。 NC 标志对此没有帮助,因为它适用于字符串值,而不是变量名称。

Is the name of HTTP_HOST case-sensitive? The RewriteCond documentation always lists it as HTTP_HOST, not http_host. The NC flag won't help with that, since it applies to the string values, not variable names.

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