使用 .htaccess 强制使用 HTTP 或 HTTPS

发布于 2024-08-26 23:14:56 字数 894 浏览 4 评论 0原文

我已经有了这段代码来强制这些 URL 为 HTTPS:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/my/?.*$
RewriteCond %{REQUEST_URI} !^/my/basket/add?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/login/?.*$
RewriteCond %{REQUEST_URI} ^/logout/?.*$
RewriteCond %{REQUEST_URI} ^/register/?.*$
RewriteCond %{REQUEST_URI} ^/newsletter/?.*$
RewriteCond %{REQUEST_URI} ^/reset-password/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

这确实很好用,但我想做的是强制任何不符合上述条件的 URL 为 HTTP,触发 301 我不想

创建一个 HTTP 页面列表并像上面的 HTTPS 那样重定向它们。安全页面 URL 将始终保持不变,除非我以某种方式更改系统,但我可以在需要时更新它们。非安全页面可以是动态的,并由此处的一些销售团队创建/编辑,因此需要智能地定位这些页面。

关于我如何做到这一点有什么想法吗?它必须使用.htaccess来完成,我一直通过在我们的PHP框架内处理它来实现这一点,但这导致了相当严重的性能问题以及问题通过我们的 Google 抓取。

干杯!

I have already have this code to force these URLs to HTTPS:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/my/?.*$
RewriteCond %{REQUEST_URI} !^/my/basket/add?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/login/?.*$
RewriteCond %{REQUEST_URI} ^/logout/?.*$
RewriteCond %{REQUEST_URI} ^/register/?.*$
RewriteCond %{REQUEST_URI} ^/newsletter/?.*$
RewriteCond %{REQUEST_URI} ^/reset-password/?.*$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

And this works really well, but what I want to do is force any URL that does not comply with the above conditions to HTTP, firing a 301.

I don't want to create a list of HTTP pages and redirect them like I have with the HTTPS above. The secure pages URLs will always remain the same, unless I change the system somehow, but I can update them when needed. The non-secure pages can by dynamic, and created / edited by some of the sales team here, therefore these need to be target intelligently.

Any thoughts on how I could do this? It has to be done using .htaccess, I have been achieving this by handling it within our PHP framework, but this has caused a pretty significant performance problem as well as causing problems with our Google crawls.

Cheers!

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

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

发布评论

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

评论(2

↙温凉少女 2024-09-02 23:14:56

对 Seidr 答案的修改,因为事实上您只想在 HTTPS 打开时进行重定向,当您希望它关闭时(而且,没有 %{HTTP} 变量)。再次,将其放在所有规则之后。

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/login/?.*$
RewriteCond %{REQUEST_URI} !^/logout/?.*$
RewriteCond %{REQUEST_URI} !^/register/?.*$
RewriteCond %{REQUEST_URI} !^/newsletter/?.*$
RewriteCond %{REQUEST_URI} !^/reset-password/?.*$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

不过还没有测试过,所以不能 100% 有效。

编辑:需要安全页面的条件,以便它们仍处于 HTTPS 模式。

A modification to the answer from Seidr, because in fact you only want to redirect if the HTTPS is on, when you want it to be off (also, there is no %{HTTP} variable). Again, put this after all your rules.

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/login/?.*$
RewriteCond %{REQUEST_URI} !^/logout/?.*$
RewriteCond %{REQUEST_URI} !^/register/?.*$
RewriteCond %{REQUEST_URI} !^/newsletter/?.*$
RewriteCond %{REQUEST_URI} !^/reset-password/?.*$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Haven't tested this though, so not 100% that it will work.

Edit: Conditions for secure pages required, so that they will still be left in HTTPS mode.

深者入戏 2024-09-02 23:14:56

当您在规则中使用 L 标志时,您应该能够仅使用 RewriteCond(如下所示)将先前条件中未捕获的任何请求重写为 HTTP?将其放在您发布的规则之后。这看起来有点太明显了,但它可能会起作用。

RewriteCond %{REQUEST_URI} ^.*$
RewriteCond %{HTTP} OFF
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]

As you are using the L flag on your rule there, you should be able to just use a RewriteCond such as the one below to rewrite any requests that have not been caught in the previous conditions to HTTP? Place it AFTER the rules you have posted. This seems a bit too obvious, but it might work.

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