简体   繁体   中英

IIS7 URL Rerwrite - how to replace all underscores with hyphens in a Regex?

I am using the URL Rewrite feature in IIS7 to turn the URL:

/main.asp?category=Name_Of_A_Product

Into:

/category/name-of-a-product/

I have created the redirect & rewrite rules below which do the majority of the work, except I cannot find a way of replacing the underscores with hyphens.

Each URL can have between zero and many underscores and I'm trying to replace them in a single regular expression, to avoid chains of 301 redirects (as I believe that is bad for SEO).

Do you know how (or if) this is can be done?

<rule name="Redirect REAL to FRIEDNLY" enabled="true" stopProcessing="true">
    <match url="^main\.asp$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        <add input="{QUERY_STRING}" pattern="^category=([^=&amp;]+)($|&amp;(.*))$" />
     </conditions>
    <action type="Redirect" url="category/{ToLower:{C:1}}/" appendQueryString="false" />
</rule>

<rule name="Rewrite FRIEDNLY to REAL" stopProcessing="false">
    <match url="^category/([^/]+)/?$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="main.asp?category={R:1}" />
</rule>

Unfortunately IIS7 has a few limitations:

  • you can only capture 9 groups C:1 ... C:9
  • there is only one string function and that's ToLower

Because of that you'll be limited to a URL with a maximum of 9 words separated by max 8 underscores (eg. /main.asp?category=One_Two_Three_Four_Five_Six_Seven_Eight_Nine ) and you'll be forced to use 9 rewrite rules:

Single Word: /main.asp?category=Product

<rule name="Redirect REAL to FRIEDNLY 1" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
    <add input="{QUERY_STRING}" pattern="^category=([A-Za-z]+)$" />
 </conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}/" appendQueryString="false" />
</rule>

Two Words: /main.asp?category=Some_Product

<rule name="Redirect REAL to FRIEDNLY 2" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
    <add input="{QUERY_STRING}" pattern="^category=([A-Za-z]+)_([A-Za-z]+)$" />
 </conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}-{ToLower:{C:2}}/" appendQueryString="false" />
</rule>

Three Words: /main.asp?category=Some_New_Product

<rule name="Redirect REAL to FRIEDNLY 3" enabled="true" stopProcessing="true">
<match url="^main\.asp$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
    <add input="{QUERY_STRING}" pattern="^category=([A-Za-z]+)_([A-Za-z]+)_([A-Za-z]+)$" />
 </conditions>
<action type="Redirect" url="category/{ToLower:{C:1}}-{ToLower:{C:2}}-{ToLower:{C:3}}/" appendQueryString="false" />
</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