How to redirect certain pages to https using Zend
Framework, how to properly add redirects to the default Zend
Framework (ZF) .htaccess file
Most Zend Framework users and consuers would admint that Using Zend
Framework is quite handy for creating large long term projects in
PHP.
However probably almost every starter with ZF like me would face
enormous problems before he understand how to manage properly
mod_rewrite based custom redirects in Zend Framework.
Recently I had a task to create a
ZF mod_rewrite custom
redirect , the task consisted in that some specific urls passed
to the webserver had to be forwarded to another
SSL protected
(https) locations
An example of what I had to do is for instance you need to redirect
all your incoming requests to a page login section like let's say
http://www.yourpage.com/login/ to
https://www.yourpage.com/login/
There is plenty of mod rewrite examples and documents writtin which
are able to achieve the up-mentioned rewrite rule, yet trying to
apply them putting a mod_rewrite redirect rules in
Zend's
default .htaccess failed to create the desired redirect.
Some of the tutorials on the subject of
URL rewritting with
mod_rewrite I've read and tried without success was:
Redirecting
URLs with Apache's mod_rewrite
.htaccess
tricks and tips .. part two: url rewritting with mod
rewrite
mod_rewrite,
a beginner's guide (with examples
Using
Apache's RewriteEngine to redirect requests to other URLS and to
https://
apache htaccess rewrite rules make redirection loop
After an overall time of 4 hours or so and many failed tries I
finally was able to determine why none of the straight ways to
url redirect http:// to
https:// urls worked. By
default my
installed zend framework .htaccess had the following
content
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I have tried to edit the below rules adding new mod_rewrite
RewriteCond(itions) and
RewriteRule(s) after the
RewriteCond %{REQUEST_FILENAME} -d code.
Like so:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTPS} !=on
RewriteRule ^login(.*) https://%{SERVER_NAME}/login$1 [R,L]
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Nevertheless the rewriterules to achieve the desired url rewrite
included after the RewriteEngine On I used I received a
404
errors instead of the expected results.
I realized that it's very likely the default zf rules being loaded
in the .htaccess are standing the way of the other rules and some
kind of interference occurs.
Therefore subsequently I decided to change the order of the mod
rewrite rules e.g. to look like in the .htaccess code I present
below:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^login(.*) https://%{SERVER_NAME}/login$1 [R,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
And oh Good heavens that piece of code finally worked and the http
to https redirect for the web site folder
http://mywebsite.com/login/* started being forwarded to
https://mywebsite.com/login/*