通过web.config将所有请求的URI重定向到(https和非www)

时间:2019-06-04 01:28:44

标签: azure web-config

我想为所有请求的URI(https和非www)重定向。任何帮助将不胜感激。谢谢,

效果很好

1)“ http”到“ https”(成功)

2)“ https”到“ https”(成功)

3)'/ index'为'/'

4)'/ contact(with www)'到'/ contact(with non www)'(成功)

<system.web>
    <customErrors mode="On" defaultRedirect="/" >
        <error statusCode="404" redirect="/" />
    </customErrors>
</system.web>

<system.webServer>
    <rewrite>

        <rewriteMaps>
            <rewriteMap name="Redirects">
                <add key="/index.php" value="/" />
                <add key="/a.php" value="/a" />
                <add key="/Res" value="/results#experience" />
                <add key="/blog/" value="/" />
            </rewriteMap>
        </rewriteMaps>

        <rules>
            <rule name="Imported Rule 1" stopProcessing="true">
                <match url="^(.*)$" ignoreCase="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    <add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
                </conditions>                   
                <action type="Rewrite" url="{R:1}.php" appendQueryString="true" />
            </rule>

            <rule name="Redirect rule1 for Redirects">
                <match url=".*" />
                <conditions>
                    <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
                </conditions>
                <action type="Redirect" url="{C:1}" appendQueryString="false" />
            </rule>

            <rule name="Remove www prefix">
                <match url="(.*)" ignoreCase="true" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="off" ignoreCase="true" />
                </conditions>
                <action type="Redirect" url="https://test.com.au/{R:1}" redirectType="Permanent"/>
            </rule>

            <rule name="Redirect index.php" stopProcessing="true">
                <match url="^(\w*/)?index\.php" ignoreCase="true"></match>
                <conditions>
                    <add input="{HTTP_HOST}" pattern="test\.com\.au$"/>
                </conditions>
                <action type="Redirect" url="https://test.com.au/{R:1}" redirectType="Permanent" />
            </rule>         
        </rules>
    </rewrite>

    <urlCompression doStaticCompression="true" doDynamicCompression="true" />

    <staticContent>
        <clientCache cacheControlCustom="public" cacheControlMode="UseExpires" httpExpires="Tue,19 Jan 2038 03:14:07 GMT" />
        <mimeMap fileExtension=".mp4" mimeType="video/mp4" />
        <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
        <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
    </staticContent>

    <defaultDocument> 
        <files> 
        <remove value="index.php" /> 
        <add value="index.php" /> 
        </files> 
    </defaultDocument>
</system.webServer>

我希望所有请求的uri仅通过web.config重定向到https和非www

1 个答案:

答案 0 :(得分:0)

请参阅以下代码:

<rewrite>
    <rules>
        <rule name="Redirect to non-www" stopProcessing="true">
            <match url="(.*)" negate="false"></match>
            <action type="Redirect" url="https://example.com/{R:1}"></action>
            <conditions>
                <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true"></add>
            </conditions>
        </rule>
    </rules>
</rewrite>