Zend Framework

Zend Framework on IIS with IIRF

Zend Framework can run on Apache, Microsoft IIS and Lighttpd. We have been running Zend Framework on all our project on Apache web server. However, one of our project informs us late about the server and that it was Windows Server 2003 and Microsoft IIS 6. We have no choice but to make Zend Framework run on IIS. It was tricky, but it works with almost no issues so far.

Update

Because of so many comments that seems like the readers are a bit confused, I have updated this post and edited parts to enhance clarity and also to integrate with the current theme. I apologize for my weird writing style.

Zend Framework’s Flexibility

Zend Frameworks search engine friendly URL actually does not need Apache’s mod_rewrite. It only needs to run its index.php and all those parameters are processed using its internal rewriting, whatever you may call it. For example:

http://localhost/myproject/login <– is the same as http://localhost/myproject/index.php/login

The first one makes use of URL rewriting via Apache (if you are on Apache) and automatically forward all requests to index.php.

The second doesn’t use Apache’s mod_rewrite and only process the parameters by its own. Using that observation, Zend Framework can live without mod_rewrite. That should also be true to Microsoft’s IIS.

IIS with ISAPI_Rewrite

Before you ever proceed, make sure you have installed PHP to your IIS. I will not cover that topic for now, so go ahead and grab the manual or search the net. It is actually not that difficult.

When I try the project on IIS, it only works on the main page. Thus, the index.php without any further parameters. Accessing index.php/login will issue 404 error. So we conclude that IIS does not treat the /login as parameter, but instead it treats that as a file.

A workaround on this rewrite issue is to use third party solutions. One of them is ISAPI_Rewrite by Helicon Tech. It actually works nice, however, the actual server is using Virtual Websites on IIS and the free version does not support it. Buying the Full Version is not an option.

Another third party solution is the Ionic’s ISAPI Rewrite Filter. It does URL rewriting that is small, free and easy to use. It is not that full of features but the basic feature is just what Zend Framework needs.

Download the Ionic’s ISAP Rewrite Filter (IIRF) here. You will only need the file: IsapiRewrite4.dll. Copy the file IsapiRewrite4.dll to anywhere on the server, but I suggest to put it on C:\WINDOWS\system32\inetsrv.

On the same folder, create the file: IsapiRewrite4.ini since the dll will look for this file at startup. Put the following content.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/(.*)$ /index.php?q=$1 [I,L]
RewriteRule ^/(.*)$ /index.php [L,I,U]

NOTE: Please note that the above rewrite rules will work only for projects that are directly under the root of the website/virtual website. If it is under a subfolder, then you need to tweak the rules to make it work.

Configurations, configurations

First, we need to set the document root to the public directory of your Zend Framework application. For example, if you save your project to c:/inetpub/wwwroot/zfapp, set the document root to c:/inetpub/wwwroot/zfapp/public. This can be done on the website property on IIS under the Home Directory tab (based on IIS 6, others may differ).

Set the two files: IsapiRewrite4.ini and IsapiRewrite4.dll permission. Make sure the IIS user’s IUSR_HOSTNAME and IWAM_HOSTNAME for Windows XP. On Windows Server 2003, the username is different. I think it was IIS_WPG or something like that. If you can’t find the correct username, look at InetPub folder and look at the permissions to give you the clues.

Only set the read access. Don’t grant write.

Set permissions

Open IIS and go to your website’s properties.

IIS Settings

Goto ISAPI Filters tab. Add the Ionic Rewriter to the filters.

ISAPI Filters

After you have done adding the filter, it should more or less look the the image above.

Path to DLL

Restart IIS and everything should work just like you were on Apache.

31 thoughts on “Zend Framework on IIS with IIRF”

  1. Thanks for dropping by. Sorry I forgot logging in to my blog, that’s why your comment is pending for a week.

    Good thing it helps you.

  2. Problem solved using correctly the rewrite rules:
    RewriteRule ^/(.*)\?(.*)$ /index.php?$2 [L,I,U]
    This way works OK!
    Thanks again!

  3. when i downloaded IonicIsapiRewriter-2.1.1.23-Release-Win32-bin.zip i got IIRF.dll but not IsapiRewrite4.dll as you have mentioned.

    I have tried all the possible ways but it didnt work. Help Please.
    Am using win2003 iis6

  4. I have done all the changes that you have mentioned above(add ini and dll) and given permissions. Placed the content in ini as mentioned above. Removed the .htaccess file which I got from default zend framework project(Does it need to be removed????).my project folder is under C:/inetpu/wwwroot). When i try to access the application its giving an error occured Application Error. Any ideas????? This application worked fine on Apache.

    Here is my .htaccess content when I implemented it on Apache.

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ – [NC,L]
    RewriteRule ^.*$ /index.php [NC,L]
    SetEnv APPLICATION_ENV development

    Please guide me in configuring this.

  5. One thing i dont understand is that even if I dont Remove isapirewrite4.dll from ISAPI filter the application is working fine. I dont know why. Even i removed isaprewrite4.ini from inetsrv folder. Any ideas?

  6. Hmmm….

    I really have no idea what’s going on with your setup Maggi. Maybe another IsapiRewrite4.dll still exists somewhere else. This is the thing I really hate when developing for Windows platform (MS platform). Things are so tricky that you spend so many hours trying to trace the problem.

    Good thing you solved it.

  7. Hi swetha

    If you are using Apache and you just wanted to redirect (not rewrite) you could try this:

    #Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

  8. Hi Lysender

    I am using IIS server and I downloaded the file isaprewrite4.dll which you have provided and placed it under inetsrv folder. Where do I write the rewrite the rewrite rules? In which file? where do I place that file? and also can you provide the Rewrite rules for zend framework.

    Thanks in advance.

  9. Hi Lysender

    I have a zend framework project with name zendapp under c:inetpub/wwwroot

    I can access this project using http://localhost/zendapp/public/index.php

    when I configured the IIRF based on the steps above I have this problem like

    when I access http://localhost/zendapp/public it is pointing to index.php file which is under c:/inetpub/wwwroot

    Do I need to change the rewrite rules?

    Is there a way that I can apply the IIRF only to the project zendapp????

    Please guide me. Thanks in advance.

  10. Hi swetha,

    Now I understand you are using IIS. Forget about the rewrite rule about apache (the one I posted on one of the comments. What you need to do instead is to point the document root to: c:/inetpub/wwwroot/zendapp/public, that way, the rewrite rule will work.

    However, that will make the zend app the only application available and will be accessible via http://localhost. If you don’t like it that way and proceed to the directory instead http://localhost/zendapp/public, you will need to change the rewrite rule.

    If the application is set at rule, the rewrite rule for IsapiRewrite4.ini is:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #RewriteRule ^/(.*)$ /index.php?q=$1 [I,L]
    RewriteRule ^/(.*)$ /index.php [L,I,U]

    However, if you want it to be in sud directory, you need to change it to something like:

    /zendapp/public/index.php

    I’m no expert in either IIS or IIRF so I cannot suggest more.

  11. Please this isapi stuff not working for me.
    i have my application in this directory c:/inetpub/wwwroot\smsapp. althought we already have other applications running on the c:/inetpub/wwwroot/. now how do i apply the Home Dicretory settings to my folder in order not to affect other applications that are runing on the server

  12. I’ve installed iis6 on windows 2003 and also php on the server. Plain php files are working fine. I tested with phpinfo.php to get the phpinfo. But when i tried to host the zend application the problem started.

    Here the application is redirecting using isapi_rewrite module to /home. But it is not going to home controller. I’m using Doctrine ORM. Is there any problem with this? I don’t think so. Moreover it is always considering “default” as the module, “index” as the controller and “index” as the action even though i’m passing any thing through the URL like http://localhost/home/index/index. But it is not even going to the Default Controller also. Can you please help me in this situation. I’m using ISAPI_Rewrite 3, that supports .htaccess. It is redirecting to my public_mvc/index.php file in the application, from there it stopped dispatching.

    Thanks in advance.

  13. Sorry Satheesh, is that the ISAPI_Rewirte by Helicon tech? I don’t have any experience on that so I cannot comment. Try using Ionic’s ISAPI Rewrite Filter and see if it works. That what this post is about.

  14. Hi lysender,
    Thanks for your quick response. I tried installing the way you have mentioned in this article (I’ve downloaded Ionic’s ISAP Rewrite Filter (IIRF)). Plain php file is running fine. Again I’m facing the same problem with zend application like, even i passed any thing in the URL giving the module=>“default”, controller=>“index” and action=>“index”. Not going inside of the controller. I’m trying to solve from past 3 days. Can you please guide me here.

  15. I’m not sure this is right place to post this question. But I’m expecting something from you people.
    When I’m passing the URL like http://localhost/home/index/index in one of my zend application I’m getting the module=>default, controller=>index, action=>index. Whatever may be the URL i’m getting the same thing. I’m running the application on IIS6 in windows 2003 platform. If i’m overriding the module as ‘home’ in Zend_Controller_Request_Abstract=>getModuleName() method then my home page is coming fine. I’m unable to get where exactly it is overriding the value to ‘default’,’index’,’index’. Can any one help me?

    Thanks in advance

  16. When I’m passing the URL like http://localhost/home/index/index in my application I’m getting the module=>default, controller=>index, action=>index. Whatever may be the URL i’m getting the same thing. I’m running the application on IIS6 in windows 2003 platform. If i’m overriding the module as ‘home’ in Zend_Controller_Request_Abstract=>getModuleName() method then my home page is coming fine. I’m unable to get where exactly it is overriding the value to ‘default’,’index’,’index’. Can any one help me?

    Thanks in advance

    Update on this problem:
    The problem is with $_SERVER[‘HTTP_X_REWRITE_URL’]. I’m getting the same URI into both $_SERVER[‘HTTP_X_REWRITE_URL’] and $_SERVER[‘REQUEST_URI’] like /public_mvc/index.php. I’m not overriding the value of $_SERVER[‘HTTP_X_REWRITE_URL’] anywhere in my code. My .htaccess code is like this.

    RewriteEngine on

    RewriteRule ^admin-mvc(.*)$ public_mvc/admin.php [L]

    RewriteCond %{SCRIPT_FILENAME} !-f

    RewriteCond %{SCRIPT_FILENAME} !-d

    RewriteRule ^(.*)$ public_mvc/index.php

    RewriteRule ^$ /home [r=301,nc]

    I’m using ISAPI Rewrite 3 for URL Rewriting.

  17. Hi Satheesh,

    Did you try to install your application into the document root? It seems not. In my application (the one I’m talking about in the post), it has only one project and is hosted on the document root.

    Try to move the document root to your public dir as first attempt to troubleshoot. Make sure that your ISAPI filter works.

    You keep mentioning that you are using Isapi Rewrite 3 but this post is about IIRF which is IsapiRewrite4. My knowledge about this IIRF is too limited so I cannot really help you that much.

    Take some time, maybe you have just missed something. It happens from time to time. This IIS+ZF integration also took me days to complete so I know the feeling.

  18. Hi lysender,
    I have uninstalled ISAPI Rewrite 3. I installed IIRF(IsapiRewrite4) as you mentioned above. I have given the document root as c:\myapplication\public_mvc. And I’ve to pass like http://localhost/index.php/home OR http://localhost/index.php/results. Now i was able to get the module, controller, action correctly and also able to see the home page and remaining pages. As I’ve mentioned the doc root as public_mvc, I placed all the scripts, styles and images folders inside of the public_mvc. Now every thing is working fine man. Thank you very much for your replies ,good support and encouragement. Even i delete the .htaccess file it is working fine. How is it possible?Here one small favour, how can i avoid passing index.php through the URL every time like http://localhost/index.php/home. Can you please guide me here?

  19. Satheesh,

    It is because IIRF does not interpret .htaccess files. The rewrite rules are read on the IsapiRewrite4.ini file instead as written in this post.

    Regarding getting rid of index.php in the URL, have you tried accessing the URL without index.php? Does it work?

    In my guess, it should work but if not, I cannot help you anymore. As what I’ve remembered, we are already contented to have that index.php in all our URLs as we are in the middle of quick migration.

    I think there is a forum for IIRF, just search around. But I really have the feeling that it is possible to get rid of index.php based on the previous comments from our fellow readers.

  20. As I set the document root to c:\myapplication\public_mvc, there is no use of IsapiRewrite4.dll file. I can’t able to use the IsapiRewrite4.ini file also. I want to use .htaccess file, because in my application i’ve seperate layout for admin panel. By using .htaccess i can able to redirect to admin folder in the application. This can be done by the following code in .htaccess that works on apache server.

    RewriteEngine on
    RewriteRule ^admin-mvc(.*)$ public_mvc/admin.php [L]
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^(.*)$ public_mvc/index.php
    RewriteRule ^$ /home [r=301,nc]

    So I can pass URL like http://localhost/index.php/admin-mvc/home that can goes to admin.php file in public_mvc folder.

    Any idea on this?

Leave a reply

Your email address will not be published. Required fields are marked *