Redirect 将非www和www(http)重定向到https

Redirect 将非www和www(http)重定向到https,redirect,apache2,debian,Redirect,Apache2,Debian,我的目标是将任何非www和www从http连接重定向到https连接,同时保留完整路径 例如: http://example.com/something/ http://www.example.com/something/ 应重定向到: https://example.com/something/ 我已经将其包含在sites available/default.conf中 RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.exam

我的目标是将任何非www和www从http连接重定向到https连接,同时保留完整路径

例如:

http://example.com/something/
http://www.example.com/something/
应重定向到:

https://example.com/something/
我已经将其包含在sites available/default.conf中

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

但它不起作用。它把我带到另一个有广告的网站(把我的域名放在地址栏里)。虽然这可能是我的DNS提供商显示的广告,因为域没有正确重定向。

请确保已启用mod rewrite

a2enmod rewrite
在启用的站点而不是可用的站点上编辑文件

对于重写规则,也许你需要这样的东西

RewriteEngine On

# add www
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI}  [L,NE,R=301]

# turn on HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}  [L,NE,R=301]

广告可能是一些打字错误,比如www.exmaple.com,甚至是www.examlpe.com而不是www.example.com,然后你的浏览器就会出现在某个广告域上。

最终成为DNS问题。我想www是缓存在旧IP上的。两天后天气转晴了。
RewriteEngine On

# add www
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI}  [L,NE,R=301]

# turn on HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}  [L,NE,R=301]