对于http和https,将非www重定向到www

时间:2017-01-19 15:35:59

标签: regex apache .htaccess mod-rewrite

我希望获得以下2页重定向www。他们各自主持人的版本;

http://website.co.uk > redirect to > http://www.website.co.uk

https://website.co.uk > redirect to > https://www.website.co.uk

我需要保持HTTP和HTTP都适用于各自的URL。

2 个答案:

答案 0 :(得分:1)

您可以使用此单一规则在wwwhttp中添加https

# for Apache 2.4+ use this rule
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

如果您使用的是较旧的Apache版本,请使用此规则:

# for Apache 2.2
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

答案 1 :(得分:0)

您是否尝试使用RewriteEngine规则? 以下应该做的伎俩(适用于vhosts 80和443):

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.website.co.uk$1 [R=301,L]
#for 80 port: RewriteRule ^(.*)$ http://www.website.co.uk$1 [R=301,L]
相关问题