别名htaccess通配符重定向http和https请求

时间:2017-05-27 17:47:18

标签: apache .htaccess redirect url-rewriting

我想将重定向http和https协议请求的所有网址重定向到https://www.example.com

example.com
example.com.au
www.example.com.au
example.co.nz
www.example.co.nz
example.co.uk
www.example.co.uk
example.net
www.example.net
example.net.au
www.example.net.au
example.org
www.example.org

我首先尝试了这个,但它并没有重定向https协议上请求的网址。

RewriteCond %{HTTP_HOST} ^example.com [OR]
RewriteCond %{HTTP_HOST} ^example.com.au [OR]
RewriteCond %{HTTP_HOST} ^www.example.com.au [OR]
RewriteCond %{HTTP_HOST} ^example.co.nz [OR]
RewriteCond %{HTTP_HOST} ^www.example.co.nz [OR]
RewriteCond %{HTTP_HOST} ^example.co.uk [OR]
RewriteCond %{HTTP_HOST} ^www.example.co.uk [OR]
RewriteCond %{HTTP_HOST} ^example.net [OR]
RewriteCond %{HTTP_HOST} ^www.example.net [OR]
RewriteCond %{HTTP_HOST} ^example.net.au [OR]
RewriteCond %{HTTP_HOST} ^www.example.net.au [OR]
RewriteCond %{HTTP_HOST} ^example.org[OR]
RewriteCond %{HTTP_HOST} ^www.example.org
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

我接下来尝试了这个,但它没有重定向包含www的网址。

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !^on
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

如何使用通配符将http和https上所有别名域名的网址重定向到https://www.example.com

更新

根据以下@CBroe评论使用!通配符试用重写规则:

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC,OR]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

更新2:

除了@starkeen评论,因为我想将http和https请求重定向到所述网址,我在htaccess中添加了2套网址。第一个是将所有http重写为https,如果它与所述URL不匹配则重定向第二个:

# Force to use HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteCond %{HTTPS} on 
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]

1 个答案:

答案 0 :(得分:1)

您可以使用:

RewriteEngine on


RewriteCond %{HTTP_HOST} !^www\.example\.com$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]