繁体   English   中英

htaccess 强制 https 并将 www 重定向到非 www,但没有其他子域

[英]htaccess force https and redirect www to non-www, but no other subdomains

我知道有很多类似的线程,但似乎没有一个与我的确切问题相符。 这是我想要做的:

(1) http://www.mydomain.com/ -> https://mydomain.com/
(2) http://mydomain.com/     -> https://mydomain.com/
(3) https://www.mydomain.com -> https://mydomain.com/

理想情况下,如果我将添加更多子域,我还想涵盖这种情况,以自动执行如下操作(最好采用适用于我添加的任何子域的通用方式)。

(4) http://sub.mydomain.com/ -> https://sub.mydomain.com/

在这一点上,我想知道是否有可能创建一个单一的 .htaccess 文件来完成我需要的一切,尽管我不得不承认我理解正则表达式,但我并不完全是一个 mod_rewrite uberpro。

以下是我已经尝试过的解决方案:

我的 SSL 证书涵盖 www-subdomain,所以我不是在寻找“不受信任的连接”错误解决方案,我知道这是不可能的。

我发现当您拥有https://www.example.com并重定向到https://example.com时,大多数建议都没有被捕获。

以下适用于所有排列:

RewriteEngine On

# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

# match urls that are non https (without the www)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

希望这可以帮助某人!

将此代码放在您的DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

强制使用 HTTPS

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

强制 www 到非 www

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.bewebdeveloper.com$
RewriteRule ^(.*) http://bewebdeveloper.com/$1  [QSA,L,R=301]
RewriteEngine on


RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

此脚本将重定向

同时保留子域。

没有一个例子对我有用,它们似乎陷入了一个循环,但下面的方法有效。

    # match any URL with www and rewrite it to https without the www
    RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
    RewriteRule (.*) https://%2%{REQUEST_URI} [R=301,L]

    # match non https and redirect to https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

顺序很重要,在某些情况下它会阻止第三次重定向。

如果您想使用子域(除 www. 之外的任何域),只需删除第一个规则集。

因此,对于子域,您只需要

    # match non https and redirect to https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

我使用 Cloudways 服务,我发现这是唯一有效的方法

我发现以下答案符合您的要求:

www非 wwwhttps没有其他子域

RewriteEngine on

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

满足所有三 (3) 个条件:

(1) http://www.example.com/ -> https://example.com/
(2) http://example.com/     -> https://example.com/
(3) https://www.example.com -> https://example.com/

但除了www没有其他子域,像这样:

(4) http://others.example.com -> https://others.example.com/
(5) https://others.example.com -> https://others.example.com/

只需将此代码添加到根 .htaccess 文件中

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

暂无
暂无

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

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