Rewrite all URL to SSL except specific folders wit

2019-07-25 07:39发布

问题:

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:

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]