Combining multiple IIS7 rewrite rules (redirection) into one

I am using the iis7 URL Rewrite module to accomplish several things:

  • 301 redirect rule from non-www to www
  • 301 redirects the .info rule to .com (moves to the .com version of my domain)
  • 301 redirects the rule from the old page, for example. / page -name.asp for page name / name

I was able to combine the first two into one rule, and the third element is my own rule. The problem is that if you request a URL, for example:

site.info/page-name.asp/

301 is executed first to:

www.site.com/page-name.asp (for example, www is added and .info goes to .com)

Then the second 301 is made from this:

www.site.com/page-name

My question is: how can I combine them so that instead of one? Here are two rules, as they are currently in my web.config:

<rule name="SEO - 301 Redirect - .info to .com AND force WWW" stopProcessing="false"> <match url="(.*)" ignoreCase="true" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_HOST}" pattern="^site\.info$" /> </conditions> <action type="Redirect" url="{ToLower:http://www.site.com/{R:1}}" redirectType="Permanent" /> </rule> <rule name=".aspVersion-to-friendlyvia301" stopProcessing="false"> <match url="(.*).asp" /> <action type="Redirect" url="{R:1}" /> </rule> 
+4
source share
2 answers

I seem to have found the answer to my question. It is a little hacked, but it does all the necessary URL conversions (for example, slash termination, non-www for www, toLowerCase, deleting the default document for directories, and any other redirects, such as changing the page name).

The problem I was talking about is actually called a "301 redirect chain", and the solution seems pretty elegant, here:

http://www.seomoz.org/blog/what-every-seo-should-know-about-iis#chaining

+6
source

This is the solution from the previous comment:

1) Instead of Redirect, apply Rewrite with an extra _

2) Add a new rule that will catch the URL starts with _ and apply the redirect

 <rule name="LowerCaseRule1" stopProcessing="false"> <match url="(.*)" ignoreCase="false" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_METHOD}" pattern="GET" /> <add input="{R:1}" pattern="[AZ]" ignoreCase="false" /> </conditions> <action type="Rewrite" url="_{ToLower:{R:1}}" /> </rule> <rule name="RemoveTrailingSlashRule1" stopProcessing="false"> <match url="(.*)/$" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_METHOD}" pattern="GET" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> </conditions> <action type="Rewrite" url="_{R:1}" /> </rule> <rule name="Final redirect" stopProcessing="true"> <match url="^(_+)(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_METHOD}" pattern="GET" /> </conditions> <action type="Redirect" url="{R:2}" /> </rule> 
0
source

Source: https://habr.com/ru/post/1440287/


All Articles