用于 HTTPS 重定向的 Apache RewriteCond RewriteRule 组合帮助

发布于 2024-10-01 15:48:18 字数 858 浏览 2 评论 0原文

需要一些帮助来将一些 RewriteRule 和 RewriteCond 组合在一起并防止重定向循环。

案例是强制所有传入请求都采用这些模式,以相同的模式重定向到 https://...

http://www.domain.com/admin --> https://www.domain.com/admin
http://www.domain.com/something/wp-login --> https://www.domain.com/something/wp-login
http://www.domain.com/something/wp-admin --> http://www.domain.com/something/wp-admin
http://www.domain.com/something/else/wp-login --> https://www.domain.com/something/else/wp-login
http://www.domain.com/something/else/wp-admin --> https://www.domain.com/something/else/wp-admin

但真正的技巧是我想用最少的语句来完成此任务。这是我到目前为止所拥有的,但它不太正确,并且在 /admin 情况下创建重定向循环,并在“wp-”情况下失败。

RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^(/admin|wp-login|wp-admin) [NC]
RewriteRule ^(.*)$ https://www.domain.com$1 [R,L]

非常感谢这里的任何指示。

干杯。

Need some help for combining some RewriteRule's and RewriteCond's together and preventing redirect loops.

Case is to force all incoming requests to these patterns, to redirect to https:// with same pattern...

http://www.domain.com/admin --> https://www.domain.com/admin
http://www.domain.com/something/wp-login --> https://www.domain.com/something/wp-login
http://www.domain.com/something/wp-admin --> http://www.domain.com/something/wp-admin
http://www.domain.com/something/else/wp-login --> https://www.domain.com/something/else/wp-login
http://www.domain.com/something/else/wp-admin --> https://www.domain.com/something/else/wp-admin

But the real trick is that I'd like to accomplish this in the fewest statements. Here's what I have so far, but it's not quite right and creates redirect loops in the /admin cases and fails in the "wp-" cases.

RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^(/admin|wp-login|wp-admin) [NC]
RewriteRule ^(.*)$ https://www.domain.com$1 [R,L]

Much obliged for any pointers here.

Cheers.

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

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

发布评论

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

评论(1

贵在坚持 2024-10-08 15:48:18

上面的评论有助于摆脱重定向循环。我还尝试以多种方式简化前面的 RewriteCond,但除了上面的方法之外,什么都无法工作。

例如..为什么下面的 RewriteCond 不做同样的事情只是更简单..

RewriteCond %{REQUEST_URI} ^/(.*)admin/?$ [NC]

在我看来这应该是以下所有内容的真实条件...

http://domain.com/admin
http://domain.com/admin/
http://domain.com/whatever/wp-admin
http://domain.com/whatever/wp-admin/
http://domain.com/whatever/somethingelse/wp-admin
http://domain.com/whatever/somethingelse/wp-admin/

但由于某种原因 wp- url 与该不匹配条件/模式。

The comment above helped get rid of the redirect loops. I also tried to simplify the preceding RewriteCond in a number of ways but couldn't get anything but the above to work.

For example..why doesn't the below RewriteCond do the same thing only more simply..

RewriteCond %{REQUEST_URI} ^/(.*)admin/?$ [NC]

Seems to me that should be a true condition for all of the following...

http://domain.com/admin
http://domain.com/admin/
http://domain.com/whatever/wp-admin
http://domain.com/whatever/wp-admin/
http://domain.com/whatever/somethingelse/wp-admin
http://domain.com/whatever/somethingelse/wp-admin/

But for some reason the wp- urls don't match that condition/pattern.

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