繁体   English   中英

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

[英].htaccess redirect non-www and non-http to https://www

我正在尝试重定向:

  • 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_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页面规则: https : //support.cloudflare.com/hc/en-us/articles/200170536-How-do-I-redirect-all-visitors-to-HTTPS-SSL-

但是您可以使用:

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM