Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache .htaccess将裸http和https重定向到www https_Apache_.htaccess_Redirect_Mod Rewrite - Fatal编程技术网

Apache .htaccess将裸http和https重定向到www https

Apache .htaccess将裸http和https重定向到www https,apache,.htaccess,redirect,mod-rewrite,Apache,.htaccess,Redirect,Mod Rewrite,我在我的一个域名上有类似的问题。我已经为裸域和www域安装了SSL证书 现在,我想重定向到裸体和 我在我的.htaccess中有这个规则,但它涵盖了当您使用显式访问站点时的重定向,但是,当访问时,它会重定向到 您应使用以下规则替换现有规则: RewriteEngine On # add www and convert http to https RewriteCond %{HTTP_HOST} !^www\. [NC,OR] RewriteCond %{HTTPS} off RewriteCo

我在我的一个域名上有类似的问题。我已经为裸域和www域安装了SSL证书

现在,我想重定向到裸体和

我在我的.htaccess中有这个规则,但它涵盖了当您使用显式访问站点时的重定向,但是,当访问时,它会重定向到


您应使用以下规则替换现有规则:

RewriteEngine On

# add www and convert http to https
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

将此规则保持在
RewriteEngine下的
行,并在测试之前清除浏览器缓存。

我个人会这样安排,这将强制使用WWW和HTTPs

RewriteEngine On

#Force WWW on everything
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

#Force HTTPS on everything
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

如果URL是
http://example.com
那么这个代码段将执行两个重定向,这对性能或seoaways学习都不好,谢谢@anubhava的输入。将不得不改变我前进的方法:在第一条规则上使用PUse-https,并且避免双重重定向。永远不要测试。
RewriteEngine On

#Force WWW on everything
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

#Force HTTPS on everything
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]