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

htaccess rewrite rules access site via IP address doesn't process rules non www to www etc

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

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)

it 301 重定向到:

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

如果我访问它:

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

it 301 重定向到:

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

如果我访问它:

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

it 301 重定向到:

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

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

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]