Michael Tobisch
Michael Tobisch

Reputation: 1098

IIS Url Rewriter: Redirect language codes

I made a web site for a client in different languages, the Urls looked like

https://www.example.com/en-US/Something/More
https://www.example.com/de-DE/Etwas/Mehr
https://www.example.com/fr-FR/Quelquechose/plus

etc.

The problem is: the client wants the language codes like /en/, /de/ and /fr/ now. No problem to change, but the pages were listed on Google quite well and lead to 404 pages now.

The site is running on IIS, it is an ASP.Net application.

Can anyone please tell me how to define a rewrite (redirect) rule to include in the web.config file that redirects all Urls like

http(s)://www.example.com/en-US[/...]

to

http(s)://www.example.com/en[/...]

I tried differnet things, but nothing worked for me. Problem is I am not good in regular expressions, sorry.

Upvotes: 0

Views: 1690

Answers (1)

Rich-Lang
Rich-Lang

Reputation: 467

Either create 3 rules that look like this:

<rewrite>
    <rules>
        <rule name="RedirectEnglish" stopProcessing="true">
            <match url="en-US(.*)" />
            <action type="Redirect" url="en{R:1}" redirectType="Temporary" />
        </rule>
    </rules>
</rewrite>

Or try something more fancy like

<rewrite>
    <rules>
        <rule name="RedirectEnglish" stopProcessing="true">
            <match url="(en|de|fr)-[A-Za-z]{2}(.*)" />
            <action type="Redirect" url="{R:1}{R:2}" redirectType="Temporary" />
        </rule>
    </rules>
</rewrite>

You'll probably need to tweak that second one and its worth learning a bit about regular expressions. Find an online regex tester and try some values and make sure the stuff that you want is matching.

Upvotes: 2

Related Questions