How to rewrite a URL in a multilingual ASP.NET website

I want to rewrite url in my asp.net multilingual website using Web.config with rules section.

I have a URL-address: http://example.com/lang/en/index.html

, http://example.com/lang/fr/index.html

etc.

I need to remove the extensions lang

and .html

to rewrite the url to: http://example.com/en/index

,http://example.com/fr/index

My Web.config:

<system.webServer>
  <rewrite>
    <rules>
        <rule name="RewriteHTML">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="{R:1}.html" />
        </rule> 
    <rule name="Rewrite friendly URLs to phsyical paths">
     <match url="^(.*)$" />
     <action type="Rewrite" url="lang/{R:0}" />
    </rule>
    </rules>
  </rewrite>
 </system.webServer>

      

So if I go to ' http://example.com/en/index ' I need to open this page: ' http://example.com/lang/en/index.html .

How can this goal be achieved?

+3


source to share


2 answers


[UPDATED] Finally, it's resolved. Here is the Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <compilation debug="false" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <customErrors mode="On" />
  </system.web>
<system.webServer>
    <rewrite>
      <rules>
        <rule name="Ignore" enabled="true" stopProcessing="true">
          <match url="^(js|css).*" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
          <action type="None" />
        </rule>
        <rule name="Redirect requests to friendly URLs">
          <match url="^(.*?)/(.*)\.html$" />
          <action type="Redirect" url="{R:2}" />
        </rule>
        <rule name="Rewrite friendly URLs to phsyical paths">
         <match url="^\/(?!#.*).*" />
         <action type="Rewrite" url="lang/{R:0}.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

      



Special thanks to Ashkan Mobayen Khiabani for "ignoring" the part (must ignore: javaScript, images and CSS folders to keep the link alive)

0


source


<rule name="the name" enabled="true" stopProcessing="true">
          <match url="example.com/(.+)(?:/(.+)?)(.html)?"/>
          <action type="Rewrite" url="lang/{R:1}/{R:2}.html"/>
</rule>

      



0


source







All Articles