.htaccess将非www和非http重定向到https://www

.htaccess将非www和非http重定向到https://www,.htaccess,url-rewriting,.htaccess,Url Rewriting,我正在尝试重定向: http://example.extension https://example.extension http://www.example.extension 到 https://www.example.extension 使用: RewriteCond %{HTTP_HOST} ^example.extension$ [OR] RewriteCond %{HTTPS_HOST} ^example.extension$ [OR] RewriteCond %{HTTP_

我正在尝试重定向:

  • http://example.extension
  • https://example.extension
  • http://www.example.extension

  • https://www.example.extension
使用:

RewriteCond %{HTTP_HOST} ^example.extension$ [OR]
RewriteCond %{HTTPS_HOST} ^example.extension$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.extension$ [NC]
RewriteRule (.*) https://www.example.extension$1 [R=301,L]
http://example.extension
被重定向到
https://www.example.extension
,但是,我收到了错误:

页面未正确重定向

如果我将
.htaccess
规则更改为:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.extension%{REQUEST_URI} [R=301,L,NE]
即使在清除浏览器缓存后,也会出现相同的症状

此外,我还有一些不想重定向到www的子域,如:

我需要
http://*.example.extension
重定向到
https://*.example.extension

除了我请求帮助的重写规则之外,
.htaccess
中唯一与Wordpress相关的内容是:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# END WordPress

# BEGIN MainWP
# END MainWP

感谢您的帮助。

没有称为
HTTPS\u主机的变量

用此规则替换您的规则:

RewriteEngine On

# main domain: add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.extension)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

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

确保完全清除浏览器缓存。

没有名为
HTTPS\u HOST的变量

用此规则替换您的规则:

RewriteEngine On

# main domain: add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.extension)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

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

确保完全清除浏览器缓存。

对于CloudFlare,必须使用CloudFlare页面规则:

但您可以使用:

RewriteEngine On

#main domain
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.extension)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

#sub-domains
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

使用CloudFlare时,必须使用CloudFlare页面规则:

但您可以使用:

RewriteEngine On

#main domain
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.extension)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

#sub-domains
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

谢谢我已将
.htaccess
规则替换为您的上述规则,即使在清除浏览器和CDN(Cloud Flare)缓存后,也会出现同样的症状。我忘记添加了我的一些子域,它们需要从
http
重定向到
https
(也就是说,我需要
http://*.example.extension
重定向到
https://*.example.extension
)。此规则来自一个实时网站,它正在工作。我正在为子域添加一个单独的规则,但首先测试主域,并确保在测试此规则时没有其他规则。谢谢。我已将上面的
.htaccess
规则替换为您的规则,即使在清除浏览器和CDN(Cloud Flare)后,也会出现相同的症状缓存。我忘了添加我有一些子域需要从
http
重定向到
https
到(即我需要
http://*.example.extension
重定向到
https://*.example.extension
)。此规则来自一个实时网站,它正在运行。我正在为子域添加一个单独的规则,但首先测试主域,并确保在测试此规则时没有其他规则。您好,Croises。这些规则是否考虑了我拥有的子域?我添加了这些规则,替换了现有规则,Firefox说:
Firefox已检测到ted注意到服务器正在以一种永远无法完成的方式重定向对此地址的请求。
。我添加了Cloudflare页面规则设置
http://*.example.extension/*
以始终使用
https
,我认为这很好。我用您的子域问题更改编辑了我的代码。您可以使用Cloudflare ru完成所有工作les,但避免在CloudFlare代理之后重定向到错误的域HI Croises。这些规则是否考虑了我拥有的子域?我添加了这些规则,替换了现有规则,Firefox说:
Firefox检测到服务器正在以一种永远无法完成的方式重定向对此地址的请求。
ave添加了一个Cloudflare页面规则设置
http://*.example.extension/*
以始终使用
https
,我认为这很好。我用您的子域问题更改编辑了我的代码。您可以使用Cloudflare规则完成所有工作,但避免在Cloudflare代理之后重定向到错误的域