.htaccess mod_rewrite:强制https和www,但不适用于本地

时间:2017-11-09 09:37:14

标签: apache .htaccess mod-rewrite

感谢Stack Overflow的用户,我在htaccess中有以下内容强制在URL中强制使用www和https。它分叉很好:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

现在 - 对于我当地的环境 - 我想要排除“local.name.de”形式的网址

根据这篇文章 Apache mod_rewrite: force www only if not in localhost我添加了以下内容:

RewriteEngine on
RewriteCond %{HTTP_HOST} !=local #new line
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

但它不起作用。我也试过这个没有成功:

RewriteCond %{HTTP_HOST} !=local.name.de
你能帮我解决这个问题吗?感谢

1 个答案:

答案 0 :(得分:0)

最后,这有效:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^local
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^local
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
相关问题