IIS URL Rewrite Default Language Path

2019-08-30 11:15发布

I'm trying to get a rule working whereby the language identifier is in the url path. We want to force the url to the en version if a language is not specified. For example:

www.domain.com/page.aspx should redirect to www.domain.com/en/page.aspx

Here's the rule we have so far, but it keeps ending up in a redirect loop.

<rule name="Default Language" stopProcessing="true">
        <match url="(.*)" />
        <conditions>                
            <add input="{REQUEST_URI}" pattern="^/(en|es|ph)/" negate="true" ignoreCase="true" />
        </conditions>
        <action type="Redirect" url="/en/{R:1}" redirectType="Permanent" />
    </rule>

Any ideas where it's going wrong?

1条回答
2楼-- · 2019-08-30 11:34

Change your rule to:

<rule name="Default Language" stopProcessing="true">
    <match url="^en/" negate="true" />
    <action type="Redirect" url="/en/{R:0}" redirectType="Permanent" />
</rule>

It will check if the url starts with en/ and if not, it will append en/ in front of the requested path.

You had an infinite redirection because whatever back reference was sent to /en/{R:1}, it was matching (.*) (as it matches anything/everything).

查看更多
登录 后发表回答