donglanfu5831 2012-06-03 18:36
浏览 53
已采纳

使用Zend Framework将特定文件夹除外的所有URL重写为SSL

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]

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

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

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

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

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.

I appreciate any help. Thanks!

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]

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

  • 写回答

1条回答 默认 最新

  • dongyilai4214 2012-06-04 21:23
    关注

    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 redirect only on specific Zend Frameworks controller/actions

    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)

    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/otherval Not 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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用