本文介绍了使用 Zend Framework 将所有 URL 重写为 SSL,但特定文件夹除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此先感谢您的帮助.我为我们的 Web 应用程序设置了 Zend 框架,并且到目前为止使用基本的 SSL 重定向工作正常.我们想将所有 URL 重定向到 SSL,除了几个路径.一种路径工作正常,只是加载图像文件.但另一条路径没有,它是一个 Zend 框架控制器,其 Action 需要请求参数.

thanks in advance for any help.I have Zend Framework setup for our web app, and has worked fine using a basic SSL redirect until now. We'd like to redirect all URL's to SSL, except a couple paths. One path works fine and is just loading image files. But the other path does not, and it is a Zend Framework Controller with an Action requiring request arguments.

这是我们当前的工作配置:

Here's our current, working config:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !(/images)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

以上适用于排除图像并允许 ZF 运行,但我们还希望排除 URI 路径/wsrv/itemdata 中的任何内容我们尝试使用这个条件:

The above works for excluding images and allowing ZF to function, but we'd like to also exclude anything in the URI path /wsrv/itemdataand we tried using this condition:

RewriteCond %{REQUEST_URI} !(/wsrv/itemdata)

/wsrv/itemdata 可能包含以下格式的几个参数:

/wsrv/itemdata may include several parameters in this format:

/wsrv/itemdata/item_nbr/111111/some_other_arg/a-value

我们的问题是它总是重定向到/index.php,它被定向到 SSL.我们需要重定向到 index.php 以使框架正常运行,但不能仅将 ssl 用于单个控制器和操作/wsrv/itemdata.

Our problem is that it always redirects to /index.php, which is directed to SSL. We need the redirect to index.php for the framework to function, but not using ssl for only the single controller and action /wsrv/itemdata.

感谢您的帮助.谢谢!

编辑 6-4-12:(已解决!大部分)它不是 URL 重写,而是我的 Zend Framework 加载器中的内容.我添加了下面的规则并使用了一些测试文件,规则有效.但是我的框架设置中的某些内容正在重定向它.

EDIT 6-4-12: (SOLVED! mostly)Its not the URL rewriting, its something in my loader for Zend Framework. I added the rules below and used some test files, the rules work. But something in my framework setup is redirecting it.

RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !(/images/)
RewriteCond %{REQUEST_URI} !(/wsrv/itemdata/)
RewriteCond %{REQUEST_URI} !(/test/make/)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

以上工作,除了/wsrv/itemdata 行,它通过框架.

The above works, except for the /wsrv/itemdata line, which is going through the framework.

推荐答案

我找到了解决问题的方法.另一个用户发布的这个问题有所帮助并且相关,但我无法让他的解决方案起作用:

I found a solution to my problem.This Question posted by another user helped and was related, but I couldn't get his solution to work:

htaccess https 重定向仅在特定 Zend Frameworks 控制器上/动作

我的解决方案奏效了,除了一个问题,这并没有阻止我继续前进.下面列出的重写规则允许我在特定的 Zend Framework 控制器和操作方法下省略 SSL 连接,同时强制任何其他不存在的东西使用正常的 SSL 连接.(基本上,强制一切 SSL,但一条路径)

My solution worked, except one issue, which didn't prevent me from moving forward. The rewrite rules listed below allow me to omit SSL connection under specific Zend Framework controller and action methods, while forcing anything else not existing to use normal SSL connections. (basically, force everything SSL, but one path)

此解决方案的唯一例外是发送的 URL 中的尾随/".如果我不发送尾部斜杠,它仍会自动将我转发到 index.php.因为无论如何我都需要传入参数,所以尾部斜杠就在那里,所以对我有用.作品:/wsrv/itemdata/作品:/wsrv/itemdata/item_nbr/123456/otherarg/otherval不工作:/wsrv/itemdata

The only exception to this solution was the trailing "/" in the URL sent. If I do not send in the trailing slash, it will still auto-forward me to index.php. Since I need arguments passed in anyway, the trailing slash is there, so works for me.Works: /wsrv/itemdata/Works: /wsrv/itemdata/item_nbr/123456/otherarg/othervalNot work: /wsrv/itemdata

这是最终的、复杂的重写规则:

Here's the final, complicated rewrite rules:

    RewriteEngine On

#if req is normal file, dir
#then pass in request unchanged (show css, js, img, etc, no ZF)
#do not proc more rules
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

#if req is ssl
#   and not wsrv/itemdata
#   and not images
#   and not test/make
# then rewrite req to index.php to use ZF
#do not proc more rules
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !wsrv/itemdata/
RewriteCond %{REQUEST_URI} !images/
RewriteCond %{REQUEST_URI} !test/make/
RewriteRule ^.*$ index.php [L]

#NOTE: itemdata/ must have trailing / in client requsts
# In other words, requesting /wsrv/itemdata will redirect to index
# I don't know how to fix that yet
# But / is always used anyway because request args are used
# Ex: /wsrv/itemdata/item_nbr/12345678

#if req is NOT ssl
#   and we want wsrv/itemdata/
#then rewrite without ssl to ZF index.php
#do not proc more rules
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} wsrv/itemdata/
RewriteRule ^.*$ index.php [L]

# Last if none above match, check for non-ssl
#if req is ssl
#   and not wsrv/itemdata
#   and not images
#   and not test/make
# then REDIRECT to https:// using same URI
#do not proc more rules
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !wsrv/itemdata/
RewriteCond %{REQUEST_URI} !images/
RewriteCond %{REQUEST_URI} !test/make/
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这篇关于使用 Zend Framework 将所有 URL 重写为 SSL,但特定文件夹除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:54