Search code examples
azureiisweb-config

URL Rewrite web.config add fake folder between domain and pages


Just a simple question. I have a domain website.com, and this page have specific pages named with country code.

  • website.com/uk
  • website.com/fr
  • website.com/us
  • ...

I've created rule on my web.config file with the following URl rewrite:

 <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
    <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="ContactUs.php?lang={R:1}" />
  </rule>

Pages are generated in PHP, variable called lang is used to display the choosen page and is present in URL.

I would like to add "contact/" between my domain and pages.

  • website.com/contact/uk
  • website.com/contact/fr
  • ...

I have the following rule :

<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
   <match url="contact([^/]+)/?$"/>
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
         <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
    <action type="Rewrite" url="ContactUs.php?lang={R:1}"/>
 </rule>

I anyone can explain me what did I didn't understand, Thanks


Solution

  • Your regular expression is slightly wrong.

    ^contact\/([a-z]+)\/?$
    

    That should do what you need it to, assuming all your country codes are letters only.