(又一个)通过在 .htaccess 中重写来删除 www 的好方法?

发布于 2024-12-08 10:13:51 字数 617 浏览 2 评论 0 原文

是的,我知道这个问题之前已经被问过 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请爱~陌生人 2024-12-15 10:13:51
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]

取自 Drupal 7 .htaccess 文件,工作起来就像一个魅力。更新了一点以添加 https 检查。

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]

Taken from the Drupal 7 .htaccess file, works like a charm. Updated a bit to add the https check.

寻找一个思念的角度 2024-12-15 10:13:51

我对上面的答案有问题,它开始重定向到 http://http:///

所以我做了一些更改,这是对我有用的代码(在 apache2 Ubuntu 服务器,VPS 服务器上)从 http://www.example.com 重定向到 http://example.com (www 到非www) 以及从 http://example.comhttps://example.com (http 到 https)

    RewriteCond %{HTTPS}s on(s)|
    RewriteCond %{HTTP_HOST} ^www\.(.+)$
    RewriteRule ^ http%2://%1%{REQUEST_URI} [R=301,L]

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).

    RewriteCond %{HTTPS}s on(s)|
    RewriteCond %{HTTP_HOST} ^www\.(.+)$
    RewriteRule ^ http%2://%1%{REQUEST_URI} [R=301,L]
无敌元气妹 2024-12-15 10:13:51

这是一个只有两行的版本,并且适用于 Apache 和 LiteSpeed

RewriteCond %{HTTPS}s-%{HTTP_HOST} ^(on(s)|offs)-www\.(.+)$ [NC]
RewriteRule ^(.*)$ http%2://%3/$1 [L,R=301]

Here's a version that is only two lines, and works on both Apache and LiteSpeed

RewriteCond %{HTTPS}s-%{HTTP_HOST} ^(on(s)|offs)-www\.(.+)$ [NC]
RewriteRule ^(.*)$ http%2://%3/$1 [L,R=301]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文