How to redirect certain pages to https using Zend Framework, how to properly add redirects to the default Zend Framework (ZF) .htaccess file

Tuesday, 20th July 2010

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 basedcustom 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 webserverhad 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 sayhttp://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 toapply 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 kindof 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/*

Share this on:

Download PDFDownload PDF

Tags: ,

2 Responses to “How to redirect certain pages to https using Zend Framework, how to properly add redirects to the default Zend Framework (ZF) .htaccess file”

  1. Amit Dave says:
    Firefox 5.0 Firefox 5.0 Windows XP Windows XP
    Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0

    Yes it worked, but I got “The page isn’t redirecting properly” error

    View CommentView Comment
    • admin says:
      Epiphany 2.30.6 Epiphany 2.30.6 Debian GNU/Linux x64 Debian GNU/Linux x64
      Mozilla/5.0 (X11; U; Linux x86_64; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/5.0 Safari/531.2+ Debian/squeeze (2.30.6-1) Epiphany/2.30.6

      Hello Amit,

      Are you sure you’re not missing something? it might be also related to apache version. On what distributions are you running this.

      My above explanations were tested and works fine on Debian Lenny.

      Best!
      Georgi

      View CommentView Comment

Leave a Reply

CommentLuv badge