我的 htaccess 中有以下内容来强制 URL 中的 www:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
如果不在本地主机上,我该如何应用它?我可以提出某种 if 条件吗?现在,我得到这样的东西:http://www.localhost/
...
我的 htaccess 中有以下内容来强制 URL 中的 www:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
如果不在本地主机上,我该如何应用它?我可以提出某种 if 条件吗?现在,我得到这样的东西:http://www.localhost/
...
RewriteCond
已经是您的“如果条件”。只需添加另一个:
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
我添加了所有这些:
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1
如果您使用的是 80 以外的端口(例如localhost:8080
),您可能还需要将其添加到正则表达式中:
RewriteCond %{HTTP_HOST} !^localhost(?::\d+)?$ [NC]
RewriteCond %{HTTP_HOST} !^127\.0\.0\.1(?::\d+)?$