简体   繁体   中英

Codeigniter URL rewrite in Web.config not working in sub directory on Windows IIS server

I have developed my website using PHP Framework - Codeigniter 2. There is a front-end and a back-end to this website.

For the back-end i have created a directory called webadmin in the root directory of my website.

This website was previously hosted on LINUX OS using APACHE server, but recently we have moved on to dedicated WINDOWS OS server using IIS server.

Previously on LINUX I was using 2 htaccess files one in the root and other in my sub-directory. It was working perfectly in both the cases.

Now due to IIS server I had to import all my settings to IIS Web.config file. Resulting web.config works perfectly for my front-end with the rewrite rule for index.php, but doesn't work in my sub-directory that is http://www.somedomain.com/webadmin .

I have used the following web.config file in my root: -

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
        <rules>
             <rule name="Clean URL" stopProcessing="true">
                <match url="^(.*)$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
            </rule>
        </rules>
    </rewrite>
        <defaultDocument>
            <files>
                <clear />
                <add value="Index.php" />
                <add value="Index.html" />
                <add value="Index.htm" />
                <add value="Index.cfm" />
                <add value="Index.shtml" />
                <add value="Index.shtm" />
                <add value="Index.stm" />
                <add value="Index.php3" />
                <add value="Index.asp" />
                <add value="Index.aspx" />
                <add value="Default.htm" />
                <add value="Default.asp" />
                <add value="Default.aspx" />
            </files>
        </defaultDocument>
        <httpErrors errorMode="Detailed" />
    </system.webServer>
</configuration>

Next is the web.config file from my sub-directory which is not working and giving me 404 error.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <security>
            <requestFiltering allowDoubleEscaping="true" />
        </security>
        <rewrite>
            <rules>
                <rule name="YOURLS" stopProcessing="true">
                    <match url="^webadmin/(.*)$" ignoreCase="false" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/webadmin/index.php/{R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

This is the version i use which works for me. Simply replace the SUBDOMAIN to yours. Remember to change the base_url to the one with subdomain in the config too.

    <?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Rewrite to index.php">
                    <match url="index.php|robots.txt|images|test.php" />
                    <action type="None" />
                </rule>
                <rule name="Rewrite CI Index">
                    <match url=".*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html|ttf|woff" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/SUBDOMAIN/{R:0}" />
                </rule>
            </rules>
        </rewrite>
     </system.webServer>
</configuration> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM