www 不会重定向到带有 https 的非 www

www doesn't redirect to non www with https on

我的问题是,当我在浏览器中键入 example.comwww.example.com 时,它会重定向到 https://example.com,但是当我键入 https://www.example.com 时,它不会重定向到 https://example.com.

我想要 url http 或 https 和 www 或非 www,它总是指向 https://example.com

我在下面使用了 url 重写 htaccess 文件。

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

提前致谢。

根据您展示的示例,请您尝试以下操作。请确保在测试您的 URL 之前清除浏览器缓存。

还要确保这应该是 .htaccess 文件中的最重要规则。

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

这应该重定向 urls,无论 url 有 http 或 https 或 url 有 www 或没有 www 到没有 www 的 https。