【发布时间】:2014-08-09 13:34:31
【问题描述】:
我需要将站点中的一些页面合并到 https。现在我想要以下两种情况。
- 如果页面 REQUEST_URI 是用户、用户/登录、用户/注册、购物车、结帐并且原始协议是 http。应该会重定向到 https。
- 如果页面 REQUEST_URI 不是用户、用户/登录、用户/注册、购物车、结帐且原始协议为 https。应该会重定向到 http。
所以我实施了以下规则。
# Block 1 - Forcing HTTPS
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{SERVER_PORT} 80
# Forcing HTTPS
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} ^/cart [OR]
RewriteCond %{REQUEST_URI} ^/checkout [OR]
RewriteCond %{REQUEST_URI} ^/user/login [OR]
RewriteCond %{REQUEST_URI} ^/user [OR]
RewriteCond %{REQUEST_URI} ^/user/register
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]
# Block 2 - Forcing HTTP
RewriteCond %{HTTPS} !=off [OR]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{SERVER_PORT} !^80$
RewriteCond %{REQUEST_URI} !^/cart
RewriteCond %{REQUEST_URI} !^/checkout
RewriteCond %{REQUEST_URI} !^/user/login
RewriteCond %{REQUEST_URI} !^/user
RewriteCond %{REQUEST_URI} !^/user/register
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
这里我遇到了问题,如果启用了两个块之一,则功能会根据规则顺利运行。但如果两者都启用,递归重定向开始发生。我需要这两种功能。如果可以通过 .htaccess 规则?
更新:在 SO 上提出的类似问题为 how in htaccess can i redirect the user to https from http and back again 。但它只有一个答案建议移到应用程序方面。
更新 2:添加 Zend Framework 应用程序默认的 .htaccess 规则
【问题讨论】:
标签: apache .htaccess redirect https rewrite