使用 mod_rewrite 强制 SSL/HTTPS [英] Force SSL/HTTPS with mod_rewrite
问题描述
我有一个 Zend Framework 应用程序,我想使用 mod_rewrite 将其强制转换为 HTTPS.当谈到 mod_rewrite 时,我很迷茫.这是我当前应用程序根目录中的 .htaccess 文件.
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
根据我所拥有的,强制应用程序进入 HTTPS 的最佳方法是什么?我已经尝试了在这里找到的几个示例,但在尝试它们时,我不断收到重定向循环或内部服务器错误.
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.
感谢您的帮助!
推荐答案
以下解决方案适用于代理服务器和非代理服务器.因此,如果您正在使用 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.
RewriteEngine On
# If we receive a forwarded http request from a proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
# ...or just a plain old http request directly from the client
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on
# Redirect to https version
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Put the rest of your rewrite rules here
这篇关于使用 mod_rewrite 强制 SSL/HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!