htaccess重定向为https,www和com到co.uk

时间:2016-03-04 15:30:57

标签: apache .htaccess redirect mod-rewrite

我已经试图解决这个问题几个小时了但现在无济于事。为了达到这一点,我拥有不同TLD,.com和.co.uk的相同域名。这是我目前的htaccess文件:

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# com to co.uk redirect
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]

我一直试图在这里测试配置:http://htaccess.mwl.be/但是我得到了一些奇怪的结果,比如.com域被附加到.co.uk域。

似乎有些规则有效,而其他规则则不然。目标是将example.com的所有变体重定向到https://www.example.co.uk,例如:

任何人都可以看到我在哪里出错吗?我对htaccess重定向不是很有经验,所以感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^ https://www.example.co.uk%{REQUEST_URI} [NE,L,R]

上述规则将重定向: