是的,我知道这个问题之前已经被问过 1000 次了...这就是区别...
我在 StackOverflow 上找到了这个答案,在这里添加了“www”:
使用 htaccess 添加带有 https 支持的 www
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
这是我见过的最美丽、最优雅的解决方案:
- 它有效
- 不需要更改即可用于特定域
- 它同时处理 HTTP 和 HTTPS 连接
- 它只有 3 行
所以,我的问题很简单。有人可以帮助我调整这段代码以使其在相反的方向上工作吗? (从网址中删除 WWW 并解决上述所有问题)?
我相信,在上述(希望是下面)解决方案之间,我们将通过 www 重写来统治它们!
Yes, I know this question has been asked 1000 times before... here is the difference...
I found this answer on StackOverflow to ADD the "www" here:
use htaccess to add www with https support
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And it is the most beautiful and graceful solution I have ever seen:
- It works
- It doesn't need to be changed to be used for a specific domain
- It addresses both HTTP and HTTPS connections
- Its only 3 lines
So, my question is simple. Can someone help me adapt this code to work in the opposite direction? (To remove the WWW from web addresses and address all the above points)?
I believe that between the above (and hopefully below) solutions, we will have the www rewrite to rule them all!
发布评论
评论(3)
取自 Drupal 7
.htaccess
文件,工作起来就像一个魅力。更新了一点以添加 https 检查。Taken from the Drupal 7
.htaccess
file, works like a charm. Updated a bit to add the https check.我对上面的答案有问题,它开始重定向到
http://http:///
所以我做了一些更改,这是对我有用的代码(在 apache2 Ubuntu 服务器,VPS 服务器上)从 http://www.example.com 重定向到 http://example.com (www 到非www) 以及从 http://example.com 到 https://example.com (http 到 https)。
I got problem with above answer, it started to redirect to
http://http:///
So I did some changes and this is code which worked for me (on apache2 Ubuntu server, VPS server) to redirect from http://www.example.com to http://example.com (www to non www) AND from http://example.com to https://example.com (http to https).
这是一个只有两行的版本,并且适用于 Apache 和 LiteSpeed
Here's a version that is only two lines, and works on both Apache and LiteSpeed