繁体   English   中英

如何使用htacces从www重定向到https www?

[英]How to redirect from www to https www with htacces?

我需要执行以下操作:我当前的地址如下: https//www.domain.com

我想用htaccess重定向:www.domain.com到https://www.domain.comhttp://domain.comhttps://www.domain.com

我在这里尝试了一些建议,但结果却是无限循环。 我试过了:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

任何帮助,将不胜感激。

更新:我想我已经完成了以下工作:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]

这是正确的方法吗?

您可以在单个规则中实现重定向,如下所示:

RewriteEngine On 

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

你应该有一个! 面前你的域名条件:

### Redirect non-www => www ###
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

因为你想说的,做下一个规则,如果这个条件匹配......和条件应该是主机符合你想要什么。

如果我把它与我以前见过和使用过的技术混合在一起,你可以试试这个:

### Redirect non-www => www ###
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

# redirect urls with index.html to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index.html HTTP/
RewriteRule ^(([^/]+/)*)index.html$ https://%{HTTP_HOST}/$1 [R=301,L]

# change // to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)//(.*) HTTP/ [NC]
RewriteRule ^.*$ https://%{HTTP_HOST}/%1/%2 [R=301,L]

最好的方法是:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

使用%{HTTP_HOST}您不需要编写域名

非WWW到WWW和http到https解决方案:

## Redirecting HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

## Redirecting non WWW to WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.tld$ [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [R=301,L]

暂无
暂无

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

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