Rewrites language selection in a multilingual website [Answered]RSS

2 replies

Last post Jan 19, 2021 07:50 AM by McKaggatt

  • Rewrites language selection in a multilingual website

    Jan 18, 2021 05:00 PM|McKaggatt|LINK

    Hi,

    I have a multilingual website. 

    The different languages are loaded depending on a parameter in the URL.

    I am struggling to write the proper web.config configuration in IIS 10 that allows me to achieve the following: 

    1. If a user access http://www.example.com/es/our-services it really loads the following page http://www.example.com/our-services.html?lang=es without changing the url in the browser
    2. If a user access http://www.example.com/our-services.html?lang=es (with the .html and parameter in it) then the url in the browser changes to http://www.example.com/es/our-services (no .html no parameter)
    3. Of course this should also work for all other pages: about-us.html, home.html, etc...

    Thanks for helping me out.

  • Re: Rewrites language selection in a multilingual website

    Jan 19, 2021 06:11 AM|Brucz|LINK

    Hi McKaggatt,

    Rewrite will not change the URL in browser but redirect will. Here are the rules which work very well in my local test.

                   <rule name="RewriteUserFriendlyURL1" enabled="true" 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="{R:2}.html?lang={R:1}" />
                    </rule>
                    <rule name="redirect" stopProcessing="true">
                        <match url="^(.*).html$" />
                        <conditions>
                            <add input="{QUERY_STRING}" pattern="lang=(.*)" />
                        </conditions>
                        <action type="Redirect" url="/{C:1}/{R:1}" appendQueryString="false" />
                    </rule>

    Best regards,

    Brucz

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
  • Re: Rewrites language selection in a multilingual website

    Jan 19, 2021 07:50 AM|McKaggatt|LINK

    Hi Brucz,

    thank you very much for this solution; it works perfectly.

    All the best!

    McKaggatt