htaccess的力量HTTPS和WWW重定向到非www,但没有其他子域 [英] htaccess force https and redirect www to non-www, but no other subdomains

查看:120
本文介绍了htaccess的力量HTTPS和WWW重定向到非www,但没有其他子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多类似的主题,但没有一个似乎符合我确切的问题。下面是我想要做什么:

I know there are many similar threads, but none of them seems to match my exact problem. Here is what I’m trying to do:

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

在理想情况下,我也想覆盖情况,如果我永远不会增加更多的子域,自动行为类似于以下(preferably在一个通用的方法,将工作的所有子域名我想补充)。

Ideally, I would also like to cover the situation if I will ever add more subdomains, to automatically behave like the following (preferably in a generic way that will work for any subdomain I add).

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

在这一点上我想知道如果它甚至可能创造,做一切我需要一个单独的.htaccess文件,但我不得不承认,我的理解正则表达式,但我不完全是一个mod_rewrite的uberpro。

At this point I’m wondering if it is even possible to create a single .htaccess file that does everything I need, although I have to admit that I understand regexes, but I’m not exactly a mod_rewrite uberpro.

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

Here are the solutions that I already tried:

  • 强制非www和https通过htaccess的
    • 排序的作品,但似乎产生重定向循环
    • 工作为(1)和(3),但不适合(2)
    • Redirects to http://mydomain.com/ (without https) for (1)
    • Does nothing for (2)
    • Redirects to http://mydomain.com/ (without https) for (3)

    我的SSL证书涵盖了www子域,所以我不是在寻找一个不可信连接的错误的解决方案,我知道这是不可能的。

    My SSL certificate covers the www-subdomain, so I’m not looking for a 'untrusted connection' error solution, I’m aware that isn’t possible.

    推荐答案

    将这个code在 DOCUMENT_ROOT /的.htaccess 文件:

    Put this code in your DOCUMENT_ROOT/.htaccess file:

    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]
    

    这篇关于htaccess的力量HTTPS和WWW重定向到非www,但没有其他子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆