htaccess 重定向到 www,不包括 https 和 http 子域

发布于 2024-09-03 09:57:37 字数 474 浏览 0 评论 0原文

这就是我目前所拥有的,我正在尝试将 http://something.com 重定向到 http://www.something.com 但如果它是一个子域,则不会执行此类操作,因此 http://other.somethings.com 将保持不变并且基于 https/http

目前有..

RewriteCond %{HTTP_HOST} !^www\.

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

So this is what I currently have, I'm trying to redirect http://something.com to http://www.something.com but if its a subdomain do no such things, so http://other.somethings.com will stay the same AND conditional based on https/http

Currently have..

RewriteCond %{HTTP_HOST} !^www\.

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

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

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

发布评论

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

评论(2

沦落红尘 2024-09-10 09:57:37

尝试此规则:

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

这只会将请求重定向到主机 example.com 并保留 HTTPS(如果使用)。

Try this rule:

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

This will only redirect requests to the host example.com and keep HTTPS if it’s used.

等往事风中吹 2024-09-10 09:57:37

虽然有点乏味,但并不是那么困难。还有另一个名为 HTTPS 的重写条件变量,即使您没有运行 mod_ssl,也可以安全使用。然后,您将链接两个 RewriteCond 检查,并得到如下所示的结果:

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

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^something\.com$
RewriteRule ^(.*)$ http://www.something.com/$1 [R=301,L]

缺点是您现在将有问题的域硬编码到 .htaccess 中。我不知道有什么方法可以在不对域进行硬编码的情况下使其工作。

It's not that difficult, although it is a bit tedious. There's another rewrite conditional variable called HTTPS which is safe to use even when you don't have mod_ssl running. You'd then chain two RewriteCond checks, and get something like this:

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

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^something\.com$
RewriteRule ^(.*)$ http://www.something.com/$1 [R=301,L]

The downside is that you are now hardcoding the domain in question into the .htaccess. I'm not aware of a way to make it work without hardcoding the domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文