.htaccess 有没有一种方法可以将非www重定向到仅在HTTP上的www,而不是HTTPS上的www?

.htaccess 有没有一种方法可以将非www重定向到仅在HTTP上的www,而不是HTTPS上的www?,.htaccess,redirect,https,url-redirection,.htaccess,Redirect,Https,Url Redirection,是否可以使用.htaccess执行以下操作: http://example.com redirecting to http://www.example.com RewriteEngine On RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L] RewriteCond %{HTTPS} o

是否可以使用.htaccess执行以下操作:

http://example.com redirecting to http://www.example.com
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,R=301,L]
但是

https://www.example.com redirecting to https://example.com
和http不重定向到https

所以基本上在HTTP上保留www,但在HTTPS上不保留www


提前感谢

您可以在站点根目录中使用这些规则。htaccess:

http://example.com redirecting to http://www.example.com
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,R=301,L]

谢谢你的回答,但它似乎不起作用(我清除了缓存和cookie)…这两个规则在我的Apache上都很好地工作。请确保您的.htaccess已启用,并且这两条规则正好位于lineOk上的
RewriteEngine下方。由于我没有在www上安装SSL,我猜安全警告消息会在重定向之前出现。谢谢你的帮助!是的,没错。SSL证书在mod_重写规则之前显示。