简体   繁体   中英

Web.Config IIS Rewrite Rules

I have a site which redirects all requests to index.php. However I need a slight change to my web.config.

This is what it currently is in the redirect section:

<rewrite>
    <rules>
        <rule name="Process" 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" />
        </rule>
    </rules>
</rewrite>

However, I need it so that if someone goes to /news then it will go to news.php instead of index.php .

What rule would I use and where would I place it? I come from an Apache background but my client is using IIS.

You can insert another rule above the existing on with a few changes to redirect to your location.

This can be done in the IIS manager as well. Doing it through this will allow you to test the syntax you use to see if it works with the urls you want.

<rule name="Rewrite-News" patternSyntax="ExactMatch" stopProcessing="true">
    <match url="news" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="news.php" appendQueryString="false" />
</rule>

So it would all look a bit like this:

<rewrite>
    <rules>
        <rule name="Rewrite-News" stopProcessing="true">
            <match url="news" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
            <action type="Rewrite" url="news.php" appendQueryString="false" />
        </rule>
        <rule name="Process" 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" />
        </rule>
    </rules>
</rewrite>

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