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

I use the Rewrite URL function in IIS7 to rotate the URL:
     /main.asp?category=Name_Of_A_Product
... to:
     / categories / product-name /

I created the redirect and rewrite rules below that do most of the work, except that I cannot find a way to replace the underscore with hyphens.

Each URL can have from 0 to many underscores, and I'm trying to replace them in one regex to avoid the 301 redirect chain (since I find this to be bad for SEO).

Do you know how (or if) this 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>
+3
source share
1 answer

, IIS7 :

  • 9 C:1... C:9
  • ToLower

- URL- 9 , 8 (, /main.asp?category=One_Two_Three_Four_Five_Six_Seven_Eight_Nine), 9 :

: /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>

: /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>

: /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>

..................

+1

All Articles