htaccess重写规则通过IP地址访问网站不会处理规则非www到www等

时间:2015-01-05 21:59:30

标签: apache .htaccess mod-rewrite redirect

到目前为止,我已经制定了以下规则,但它并没有像我希望的那样正常工作。

Options +FollowSymLinks
RewriteEngine On
# (1) Redirect http to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# (2) Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# (3) Direct access through IP
# CHANGE 11.11.11.11 TO YOUR ACTUAL IP
RewriteCond %{HTTP_HOST} ^11\.11\.11\.11$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我想要实现的是,如果我通过其IP地址访问我的网站(例如:11.11.11.11),则不会出现以下规则:

如果我以下列方式访问它:

http://example.com.au/(subfolders/etc)

它301重定向到:

https://www.example.com.au/(subfolders/etc)

如果我以下列方式访问它:

http://www.example.com.au/(subfolders/etc)

它301重定向到:

https://www.example.com.au/(subfolders/etc)

如果我以下列方式访问它:

https://example.com.au/(subfolders/etc)

它301重定向到:

https://www.example.com.au/(subfolders/etc)

1 个答案:

答案 0 :(得分:0)

删除导致重定向循环的最后一条规则。而是将此规则添加到顶部:

RewriteCond %{HTTP_HOST} ^11\.11\.11\.11$
RewriteRule ^ - [L]

或者将此条件添加到您的其他两个规则中:

Options +FollowSymLinks
RewriteEngine On
# (1) Redirect http to https
RewriteCond %{HTTP_HOST} !^11\.11\.11\.11$
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# (2) Redirect non-www to www
RewriteCond %{HTTP_HOST} !^11\.11\.11\.11$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]