Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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 如何重定向https://example.com/ 到https://www.example.com/_Apache_Mod Rewrite_Redirect - Fatal编程技术网

Apache 如何重定向https://example.com/ 到https://www.example.com/

Apache 如何重定向https://example.com/ 到https://www.example.com/,apache,mod-rewrite,redirect,Apache,Mod Rewrite,Redirect,我的.htaccess文件中有此代码,如下所示 RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 这是为了将http重定向到https而添加的,因此整个站点现在都是https版本 现在我想从非www重定向到www。当我键入example.com时,我应该被重定向到 如何使用.htaccess文件执行此操作?将以下行放入.htaccess文件中,并用要重定向

我的
.htaccess
文件中有此代码,如下所示

RewriteEngine On
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
这是为了将http重定向到https而添加的,因此整个站点现在都是https版本

现在我想从非www重定向到www。当我键入example.com时,我应该被重定向到


如何使用
.htaccess
文件执行此操作?

将以下行放入.htaccess文件中,并用要重定向的域替换
mydomain.com

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.){0,1}mydomain.com$ [OR]
RewriteCond %{HTTPS_HOST} ^(www\.){0,1}mydomain.com$
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

要使其工作,您必须从.htaccess文件中删除您在问题中提到的现有条目

非SSL重定向:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com$1 [R=permanent,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ https://www.domain.com$1 [R=permanent,L]
SSL重定向:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com$1 [R=permanent,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ https://www.domain.com$1 [R=permanent,L]

我可能有解决办法:

RewriteEngine  on

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.[domain].com%{REQUEST_URI} [R=301,L,NE]

替换[domain]和您自己域的扩展。如果这不起作用,告诉我

如果https使用端口443,该怎么办

RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

不,劳伦斯·伍德曼先生,用上面的代码,我的网站重定向到……>这也是我自己尝试上述代码时所面临的问题。它只是重定向到其他地方。。。。。这里有技术人才吗?我认为它需要对htaccess文件中已经存在的代码进行一些操作..>RewriteCond%{HTTPS}>off RewriteRule(.*)>HTTPS://%{HTTP_HOST}%{REQUEST_URI}@jeni您描述的问题可能是由双重重定向引起的。如果您只是将我在回答中提到的代码放在您的.htaccess文件中,而不使用现有代码,那么它应该可以工作。您所说的非SSL和SSL重定向是什么意思。在我的例子中,apache服务器位于负载平衡器后面,从负载平衡器发送到服务器的所有请求都是端口80上的HTTP