Htaccess and CakePHP 2 on windows IIS7

2019-07-20 10:03发布

问题:

I was having problems trying to configure the mod rewrite over windows and II7: CakePHP 2.2.2 not working on Windows IIS7 but finally i could import the htaccess in order to create the web.config file for IIS7.

The thing is: Now that file has been created in cakephp folder and i can access to the main page BUT it has not been created inside app/ or app/webroot in which you can find 2 more .htaccess files.

Now, i can not access any other view than the main site, it shows a 404 page not found error and i am pretty sure that it is because it is not getting those .htaccess files on the web.config.

My cakephp web.config file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="Imported Rule 1" stopProcessing="true">
                <match url="^$" ignoreCase="false" />
                <action type="Rewrite" url="app/webroot/" />
            </rule>
            <rule name="Imported Rule 2" stopProcessing="true">
                <match url="(.*)" ignoreCase="false" />
                <action type="Rewrite" url="app/webroot/{R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
</configuration>

Although the CakePHP documentation tells you to add a different code (which makes the views work but no styles are loaded. And the home page don't work.) http://book.cakephp.org/2.0/en/installation/advanced-installation.html#url-rewrites-on-iis7-windows-hosts

When trying to access a CSS file from the URL i get this message:

Missing Controller

Error: CssController could not be found.

Error: Create the class CssController below in file: app\Controller\CssController.php

<?php
class CssController extends AppController {

}

Any idea? Working with Cakephp over windows is driving me crazy...

回答1:

I should mention that my response is an improvement on the solution posted here: http://www2.palomar.edu/pages/sphillips/cakephp-with-iis-7-rewrite-rules-in-a-sub-folder/

A simpler and more flexible fix would be to get rid of /{Path_To_CakePHP_Directory}/ altogether, including the forward slash (/). By keeping the paths relative, your project folder becomes a lot more mobile. Here's what the web.config would look like:

 <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>


回答2:

Ok, finally i get it work using this web.config i found on this site: http://www2.palomar.edu/pages/sphillips/cakephp-with-iis-7-rewrite-rules-in-a-sub-folder/

I just changed the /{Path_To_CakePHP_Directory}/ for /.