简体   繁体   中英

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

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.

Do you want to first redirect website.com/uk to website.com/contact/uk and rewrite website.com/contact/uk to ContactUs.php?lang=uk?

if you wnats to do like that you could use below URL rewrite rule:

<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
   <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
                        <add input="{HTTP_HOST}" pattern="www.sample2.com" />
                        <add input="{REQUEST_URI}" pattern="^/[a-zA-Z]+$" />
      </conditions>
    <action type="Redirect" url="http://{HTTP_HOST}/contact{C:0}" />
 </rule>
              <rule name="RewriteUserFriendlyURL2" stopProcessing="true">
   <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_URI}" pattern="/contact/([a-z]+)" />
      </conditions>
    <action type="Rewrite" url="ContactUs.php?lang={C:1}" />
 </rule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM