CakePHP | URL Rewrites on IIS7 (Windows hosts / Azure)





IIS7 does not natively support .htaccess files. While there are add-ons that can add this support, you can also import htaccess rules into IIS to use CakePHP’s native rewrites. To do this, follow these steps:




1. Create a new file in your CakePHP root folder, called Web.config.
     /site/wwwroot/    or  /site/wwwroot/cakefolder/ or outside of your app   directory

2. Paste this rule to your Web.config.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear/>
                <rule name="Imported Rule 0" stopProcessing="true">
                    <match url="^(img|css|files|js)(.*)$"></match>
                    <action type="Rewrite" url="app/webroot/{R:1}{R:2}" appendQueryString="false"></action>
                </rule>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
                </rule>
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="^$" ignoreCase="false" />
                    <action type="Rewrite" url="app/webroot/" />
                </rule>
                <rule name="Imported Rule 3" stopProcessing="true">
                    <match url="(.*)" ignoreCase="false" />
                    <action type="Rewrite" url="app/webroot/{R:1}" />
                </rule>
                <rule name="Imported Rule 4" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>


 
If your using other directory in your webroot beside img, css, files, js. 
You should add it to the rule in the
 
<match url="^(img|css|files|js)(.*)$"></match>
 
Example: 
  
 <!-- Responsive -->
 <?php echo $this->Html->css('../bootstrap/css/bootstrap.min'); ?>
 <?php echo $this->Html->css('../bootstrap/css/bootstrap-responsive.min'); ?>
 
You will have error BootstrapController is missing here.
 
 <!-- validation -->
 <?php echo $this->Html->script('../lib/validation/jquery.validate.min'); ?> 

You will have error LibController is missing here.
 
 
To resolve these problem you should add it to the rule above in line
 
<match url="^(img|css|files|js|lib|bootstrap)(.*)$"></match>


Learn more here: http://book.cakephp.org/2.0/en/installation/url-rewriting.html

Comments