重定向 www.到非 www 并使用 .htaccess 将所有 http 重定向到 https [英] Redirect www. to non-www and redirect all http to https with .htaccess

查看:42
本文介绍了重定向 www.到非 www 并使用 .htaccess 将所有 http 重定向到 https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问了数百万次的问题,但在阅读了 SO 上的许多答案后,我仍然无法弄清楚.我需要重定向所有请求,如下所示:

Million times asked question, but after reading many of the answers here on SO I still can't figure this out. I need to redirect all requests like below:

  1. http://domain.tld > https://domain.tld
  2. http://www.domain.tld > https://domain.tld
  3. https://www.domain.tld > https://domain.tld
  4. http://sub.domain.tld > https://sub.domain.tld(当 .htaccess 放在子域文件夹中时)
  1. http://domain.tld > https://domain.tld
  2. http://www.domain.tld > https://domain.tld
  3. https://www.domain.tld > https://domain.tld
  4. http://sub.domain.tld > https://sub.domain.tld (when .htaccess placed in subdomain folder)

直到现在,我都在使用 html5boilerplate 中的代码,它解决了 www 到非 www 的问题

Until now, I was using the code from html5boilerplate, that solved www to non-www

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/ [R=301,L]
</IfModule>

他们也有一个用于 http 到 https 重定向的代码,但是在添加这段代码后(在 www 重定向之上),页面正在加载,超时后显示错误/重定向过多

they also have a code for http to https redirect, but after adding this piece of code (above the www redirect), page is loading and after a timeout it shows error/too many redirects

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

我也试过这样的例子 this SO回答,但该网站仍然无法正常工作.目前唯一的解决方案是使用第一段代码并将 http 替换为 https 但这并不能解决最重要的重定向 (1)

I've also tried examples like this SO answer but still the website doesn't work. Only solution for now is to use the first piece of code and replace http with https but that doesn't solve most important redirect (1)

当前完整的.htaccess内容:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)\/$ /$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]

    #RewriteCond %{HTTPS} off [OR] #by uncommenting this, site stops working
    RewriteCond %{HTTP_HOST} ^www
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
    RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
</IfModule>

推荐答案

有时,根据某些服务器配置,HTTPS 变量似乎始终设置为 off.这将解释循环,我已经在 SO 上看到了类似的案例.

Sometimes, depending on some server configuration, it appears that HTTPS variable is always set to off. That would explain the loop, and i've already seen a similar case here on SO.

一个简单的解决方法是测试 SERVER_PORT(如果您使用默认的 80 和 443),或者您也可以测试 HTTP:X-Forwarded-Proto.

A simple workaround would be to test SERVER_PORT (if you use default 80 and 443) or you could also test HTTP:X-Forwarded-Proto.

示例(1)测试是否不是 https:

Example (1) to test if it is not https:

RewriteCond %{HTTP:X-Forwarded-Proto} !https

示例(2)测试是否不是 https:

Example (2) to test if it is not https:

RewriteCond %{SERVER_PORT} !403

这篇关于重定向 www.到非 www 并使用 .htaccess 将所有 http 重定向到 https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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