Peter Viola
  • Linkedin
  • Twitter
  • RSS
  • IIS
  • ASP.NET
  • Windows Server
    • Windows Server 2012
    • Windows Server 2008
    • SQL Server
  • WordPress
  • Email
  • Windows 10
Search the site...
  • Home
  • IIS
  • IIS 7 Url Rewrite Rules for SEO and Security

IIS 7 Url Rewrite Rules for SEO and Security

September 13, 2012 / Peter Viola / IIS
Share on Facebook Share
Share on Twitter Tweet
Share on Pinterest Share
Share on Linkedin Share
Share on Digg Share

Before IIS 7, if you wanted to do url rewriting with IIS 6 you had to use a 3rd party program such as ISAPI Rewrite by helicontech.com. This was a good program but it wasn’t native to IIS and there were limitations such as a site hosting more than 1 domain with different applications running.

\r\n

With IIS 7 url rewriting and redirecting has never been easier thanks to Microsoft’s Url Rewrite module. The rewriting is done by rules which are specified in the web.config under <system.webserver> element. Using IIS Manager you can use the Url Rewrite gui to create and maintain your rules.

\r\n


\r\n

You can also just put the rules directly into the web.config without using the gui. For example:

\r\n

<system.webserver>
<rewrite>
<rules>
<rule name="xyz">...blah...</rule>
</rules>
<rewrite>
</system.webserver>
\r\n

\r\n

IIS 7 Url Rewrite WWW

\r\n

\r\nOne of the most common needs for SEO is to force your site to use www for all page requests so that search engines will go to www.mydomain.com instead of domain.com. This is very easy to do with IIS 7’s Url Rewrite. Here is the rule:

<rewrite>
<rules>
<rule name=”Redirect to www” patternSyntax=”Wildcard” stopProcessing=”true”>  
<match url=”*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”peterviola.com” />
  </conditions>
 <action type=”Redirect” url=”http://www.peterviola.com/{R:0}” />
</rule>
</rules>
<rewrite>

\r\n

This works really well and it is a completely seamless experience for your web site visitors.  Here is how the rule looks in the IIS Manager gui.

IIS 7 Url Rewrite HTTP to HTTPS

\r\nProbably the 2nd most common use of Url Rewrite is for sites that have SSL certificates installed and need to seamlessly redirect page requests using the certificate for either the entire site or a particular folder. Here is the Url Rewrite rule for redirecting requests on the entire site. You simply detect if the request is not secure and then redirect to the secure channel:

<rewrite>
<rules>
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

IIS 7 Url Rewrite HTTP to HTTPS on Subfolder

The example above is great but running your entire site in HTTPS will have a performance impact so you don’t need to do it unless there is a specific business requirement for it. So then we need a rule to redirect requests to HTTPS for just one folder. In this example we’ll use a folder called “/secure”. In this instance we use the same rule as above however now we only want page requests for the “secure” folder. This is done by modifying the “match url” element.

<rewrite>
<rules>
<rule name="HTTPS on subfolder" enabled="true">
<match url="(^secure/.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rules>
<rewrite>

We’ve covered 3 of the most common uses of IIS 7 Url Rewrite but if you notice the rules above are really for redirecting and not url rewriting. We’ll cover more examples on rewriting in an upcoming post.

Avatar

Peter Viola

Creative, customer focused, results oriented, Senior Web Systems Engineer who enjoys providing the highest level of customer service supporting complex Windows hosting solutions. MCITP, MCSA, MCTS

More Posts - Website

IIS, IIS 7, Url Rewrite

Recent Posts

  • Using C# to Automate Java Keytool Certificate Signing Requests
  • Using C# to Modify Windows Server Environment Variables
  • How to Integrate a Mailchimp Mailing List with WordPress
  • The Best PayPal WordPress Plugin I Have Ever Used
  • How to Securely Erase Hard Drives

Categories

  • ASP.NET
  • Email
  • IIS
  • SmarterTools
  • SQL Server
  • Windows 10
  • Windows Server
  • Windows Server 2008
  • Windows Server 2012
  • Wordpress

Tags

301 redirect Advanced Intellect asp.net backups c# disk space DTA Dynamic IP Restrictions email email security Fiddler FTP FTP Logon Attempt Restrictions FTP Security IIS IIS 6 IIS 7 IIS 8 Log Parser optimization Performance PHP Powershell SEO Server Core Smartermail Smartertools SMTP spam filtering SQL Injection SQL Profiler SQL Server SSL StressStimulus Task Scheduler Url Rewrite vbscript robocopy vmware Web PI Windows 8 Windows Server 2008 Windows Server 2012 Wordpress wpforms WPI
(c) 2020 Peter Viola
X
Subject:
Message: