使用 mod_rewrite 强制使用 SSL/HTTPS
我有一个 Zend Framework 应用程序,我想使用 mod_rewrite 将其强制转换为 HTTPS。当谈到 mod_rewrite 时我非常迷失。这是我的应用程序根目录中当前的 .htaccess 文件。
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule !\.(html|htm|php|js|ico|gif|jpg|png|css|csv)$ /subdir/index.php
根据我的情况,强制应用程序进入 HTTPS 的最佳方法是什么?我已经尝试了在这里找到的几个示例,但是当我尝试它们时,我不断收到重定向循环或内部服务器错误。
感谢您的帮助!
I have a Zend Framework application that I want to force into HTTPS using mod_rewrite. I am pretty lost when it comes to mod_rewrite. Here is my current .htaccess file in the root of my application.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule !\.(html|htm|php|js|ico|gif|jpg|png|css|csv)$ /subdir/index.php
What is the best way to force the application into HTTPS based on what I have? I have tried a couple of the examples I found on here, but I keep getting redirect loops or internal server errors when I try them.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也在寻找这个解决方案。我添加了 Gumbo 的解决方案,它对我来说非常有效。但我的原版不一样。我的网站/应用程序重写通过index.php(对于 fany url 和../application)重定向所有内容,除了您特别请求的文件。 (就像公共根目录中的图像或其他静态文件)这是我的原件,我很久以前在 ZF 网站上找到的。
这是 Gumbo 的附加规则,用于在整个站点上强制使用 SSL。到目前为止对我来说效果很好。
I was looking for this solution as well. I added Gumbo's solutions and it worked great for me. But my original was different. My site/app rewrite redirects everything through index.php (for fany urls and ../application) except for files you request specifically. (like an image or other static files in the public root) Here's my original, which I found off the ZF site a long time ago.
Here's with Gumbo's additional rules to force SSL on the whole site. So far works good for me.
以下解决方案适用于代理服务器和非代理服务器。因此,如果您使用 CloudFlare、AWS Elastic Load Balancing、Heroku、OpenShift 或任何其他云/PaaS 解决方案,并且您在正常 HTTPS 重定向中遇到重定向循环,请给它一个尝试。
The following solution works for both proxied and unproxied servers. So if you are using CloudFlare, AWS Elastic Load Balancing, Heroku, OpenShift or any other Cloud/PaaS solution and you are experiencing redirect loops with normal HTTPS redirects, give it a try.
将此规则放在当前规则之前:
Put this rule before your current rules: