简体   繁体   English

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

[英]How to Redirect https://example.com/ to https://www.example.com/

I have this code in my .htaccess file as shown below. 我在.htaccess文件中有此代码,如下所示。

RewriteEngine On
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

This was added to redirect http to https so the entire site is now in https version. 添加此选项是为了将http重定向到https,因此整个站点现在都是https版本。

Now I want to redirect from non-www to www. 现在我想从非www重定向到www。 When I type example.com I should be redirected to https://www.example.com/ 当我输入example.com时,我应该被重定向到https://www.example.com/

How do I do that with an .htaccess file? 如何使用.htaccess文件执行此操作?

Non-SSL redirection: 非SSL重定向:

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

SSL redirection: SSL重定向:

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

How about this as https uses port 443 这个怎么样,因为https使用端口443

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

I might have the solution: 我可能有解决方案:

RewriteEngine  on

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

Replace [domain] and the extension to your own domain. 将[domain]和扩展名替换为您自己的域名。 Tell me if this doesn't work. 告诉我,如果这不起作用。

Put the following lines in to your .htaccess file and replace mydomain.com with the domain that you want to redirect. 将以下行放入.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]

For this to work you will have to remove the existing entries that you mention in your question from the .htaccess file 为此,您必须从.htaccess文件中删除您在问题中提到的现有条目

暂无
暂无

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

相关问题 如何将 https://www.example.com/aaa 重定向到 https://example.com/aaa/ - how to redirect https://www.example.com/aaa to https://example.com/aaa/ htaccess重定向https:// www。 (https://www.example.com)到非www(https://example.com) - htaccess redirect https://www. (https://www.example.com) to non www (https://example.com) 如何将 http://www.example.com 重定向到 https://example.Z4D236D9A2D102C5ZFE6AD1Cware - How to redirect http://www.example.com to https://example.com with middleware Apache2 使用虚拟主机将 https://example.com 重定向到 https://www.example.com - Apache2 redirect https://example.com to https://www.example.com with vhosts http://example.com不会重定向到https://www.example.com - http://example.com would not redirect to https://www.example.com 解析 http://www.example.com 以通过 htaccess 永久重定向到 https://example.com - Resolving http://www.example.com to permanently redirect to https://example.com via htaccess 全部重定向到 https://www.example.com - Redirect all to https://www.example.com 将* .example.com和example.com重定向到HTTPS的www.example.com时出现问题 - Problem with redirecting *.example.com & example.com to www.example.com for HTTPS 如何使 htaccess 强制 https 用于 www.example.com 和 example.com 但没有其他子域 - how to make htaccess force https for www.example.com and example.com but no other subdomains 强制从 https://example.com 重定向到 https://www.example.com - force redirection from https://example.com to https://www.example.com
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM