Apache HTTPS从非www重定向到非www

Apache HTTPS从非www重定向到非www,apache,.htaccess,Apache,.htaccess,我有一个非WWW域,当我尝试设置重定向到HTTPS时,重定向到对我有效,但对我无效。当前访问权限为: RewriteEngine On RewriteCond %{REQUEST_URI} .*index\.html/?$ [NC] RewriteRule ^(.*)index\.html/?$ https://$1 [R=301,L,NC] RewriteCond %{HTTP_HOST} ^www\. [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)

我有一个非WWW域,当我尝试设置重定向到HTTPS时,重定向到对我有效,但对我无效。当前访问权限为:

RewriteEngine On
RewriteCond %{REQUEST_URI} .*index\.html/?$ [NC]
RewriteRule ^(.*)index\.html/?$ https://$1 [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond $1 !index\.php 
RewriteCond $1 !^assets
RewriteCond $1 !^robots.txt
RewriteCond $1 !^sitemap.xml
RewriteRule ^(.*)$ index.php [L]
你能告诉我我做错了什么吗?多谢各位

更新:

当前.htaccess

RewriteEngine On 
RewriteCond %{REQUEST_SCHEME} =http 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301] 
RewriteCond %{REQUEST_URI} .*index\.html/?$ [NC] 
RewriteRule ^(.*)index\.html/?$ https://$1 [R=301,L,NC]
RewriteCond $1 !index\.php 
RewriteCond $1 !^assets 
RewriteCond $1 !^robots.txt 
RewriteCond $1 !^sitemap.xml 
RewriteRule ^(.*)$ index.php [L]

您将此规则用于
https
重定向:

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
第一个条件是
%{HTTP\u HOST}^www\.
,这意味着它将只匹配以
www.
开头的主机名,因此您不会得到
http://example.com/

您可以改用此重定向规则:

RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
这将执行以下重定向:

  • http://example.com/ => https://example.com/
  • http://www.example.com/ => https://www.example.com/

  • 在测试之前,请确保清除浏览器缓存。

    否,仍然存在相同的问题:(此外,请保留我的建议规则作为您最重要的规则,就在
    RewriteEngine
    行的下方,并清除浏览器缓存。对不起,我更新了原始帖子。仍然不起作用。您的Apache版本是什么?我可以知道您的实际域名以供测试吗?因此我会询问托管公司。非常感谢您。)