简体   繁体   中英

IIS Rewrite Rule for Lower Case URL's

I have a IIS Rewrite Rule to convert all URL's to lower case

<rewrite>
    <rules>
        <rule name="LowerCaseRule1" stopProcessing="true">
            <match url="[A-Z]" ignoreCase="false" />
            <action type="Redirect" url="{ToLower:{URL}}" />
        </rule>
    </rules>
</rewrite>

However it only converts a part of the url to lowercase and does not convert querystrings. For example ID is not coverted to id

http://www.itsmysitesitesite.com/showproduct.aspx?ID=230

How can I modify the above rule to include query strings too?

Based on my comment (referencing this question) I would imagine the rule would look something like this:

<rule name="URL Lower" enabled="true" stopProcessing="true">
      <match url="[A-Z]" ignoreCase="false" />                        
      <conditions trackAllCaptures="true">
          <add input="{QUERY_STRING}" pattern="(.*)" />
          <add input="{QUERY_STRING}" pattern="([A-Z]+)" ignoreCase="false" />
      </conditions>
      <action type="Redirect" url="{ToLower:{URL}}{ToLower:{C:1}}" appendQueryString="false" />
 </rule>

A disclaimer though, I'm working on Windows XP so I've only got IIS 6.0, so I can't check the syntax is correct! Might require a bit of tweaking...

I wasn't able to make one rule that would capture both the {URL} and {QUERY_STRING} and also display the url the way I wanted it to. So, I split it into two rules.

URL to Lowercase

<rule name="UrlToLowercase" stopProcessing="true">
  <match url=".*[A-Z].*" ignoreCase="false" />
    <conditions>                          
      <add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" />
      <add input="{URL}" pattern="^.*\.(axd|css|js|jpg|jpeg|png|gif|txt|xml|svg|pdf)$" negate="true" ignoreCase="true" /> 
    </conditions>
    <action type="Redirect" redirectType="Permanent" url="{ToLower:{URL}}" />
</rule> 

URL with Query String to Lowercase

<rule name="UrlWithQueryStringToLowercase" stopProcessing="true">
  <match url="(.*)" ignoreCase="false" />
    <conditions trackAllCaptures="true">   
      <add input="{QUERY_STRING}" pattern="(.*)" /> 
      <add input="{QUERY_STRING}" pattern=".*[A-Z].*" ignoreCase="false" />
      <add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" />
    </conditions>
    <action type="Redirect" redirectType="Permanent" url="{ToLower:{URL}}?{ToLower:{C:0}}" appendQueryString="false" />
</rule>

This should cover every case and if it doesn't, please let me know. :)

I added some extra conditions that you may want to leave in or take out.

<add input="{URL}" pattern="^.*\\.(axd|css|js|jpg|jpeg|png|gif|txt|xml|svg|pdf)$" negate="true" ignoreCase="true" /> ignores paths that end in axd, css, js, jpg, ...etc. Without this rule, you will not be able to load a file with uppercase letters from your server.

<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" /> will ignore requests with a POST method. Without this rule, POSTs that specify uppercase urls will lose data because of the 301 (GETs do not have this problem).

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