There are lots of routing options accessible in ASP.NET but still it comes a time when you need to manipulate a URL and manipulating it outside a code comes handy. When this happens, the best you can do id to use IIS Rewrite Module. Transforming various URL’s out of code enables you to do various things including performing redirections for archive or transferred content without interfering with the code, you can easily implement SEO optimizations and tweaks quickly and easily without code and many more.Below is a collection of useful IIS rewrite rules that will help you understand IIS rewrites.
Useful IIS Rewrite Rules
Adding www Prefix
This is a basic rule that adds prefix “www” to any URL you need. This is a requirement for SEO.
Removing Prefix www
This is the opposite of the first rule above. You can use this rule to remove prefix www from any URL.
Domain 1 to Domain 2 Redirect
This rule comes handy when you change the name of your site or may be when you need to catch and alias and direct it to your main site. If the new and the old URLs share some elements, then you could just use this rule to have the matching pattern together with the redirect target being
This rule comes handy in the event that you reorganize your site and do not want to get rid of old and removed URLs. You may want to send the old URLs to a replacement page. Look at the example below where a number of old pages have been directed to the services page.
Specific Subdomain Redirection
Removing a deprecated site/blog requires you to apply this rule by matching a subdomain and then sending it to a different path. For instance, you can redirect blog.mysite.com/someentry/ to mysite.com/blog/entry/ whenever you require. This is mostly done for SEO reasons. To the site’s bindings of the destination, you will need to add the old blog domain hostname in the example given above to complete the process.
You may want to redirect content conditional on the name of the domain. This rule is almost similar to the rule above with the only difference being that it’s applied in a different case. If you have many sites that are linked to one domain and need a “preview” URL to see the functionality of the product while ensuring the site is disabled for those visiting through livesite.com, then you can use this rule. Users will need the secret domain of the preview site to hit the URL of the product
Sitemaps That Are Domain Conditional
This rule applies when you have one site that is serving many domain names but you need a specific Sitemap per domain. This not like a redirect and is more of a rewrite and edits the URL as you need. In the example below, you can alter the URL matches on NZ/AU domain suffixes as you require. This rule can also be applied on robot.txt files that are domain specific.
The default.ASPX in most ASP.NET sites is always “/” which is a way of having a simple and nice looking URL which makes the URL visually appealing. You can change that with this rule.
Redirecting users from HTTP to HTTPS is one of the reasons that you need to apply useful IIS rewrite rules. It can lead to conditional statements while looking for dev/test mode in your code. This rules allows you to handle the redirection without much statements which is tidier.
There is a pair of rules in this case each for one of the two ways. In both the rules, a check is performed to verify that the protocol used is http/https. The rules work on the same URL patterns or the similar lists of pages to match. For the redirect to HTTP, it is not about matching the pages; it is a reverse of the first rule and usually have a number of .NET/site paths that are excluded.
How do you Redirect a Subdomain to a Subdirectory
By use of ASP.net or ASP, it is easy redirecting a subdomain to a subdirectory. The requests will be redirected by use of an ASP file, the directory name and the domain name will show in the browser’s URL to where the request has been sent. Requests can also be directed to a specific file.
http://subdomain2.HostingAccountDomain.com –> http://subdomain2.HostingAccountDomain.com/subdomain2
http://subdomain1.HostingAccountDomain.com –> http://subdomain1.HostingAccountDomain.com/subdomain1
http://subdomain3.HostingAccountDomain.com –> http://subdomain3.HostingAccountDomain.com/subdomain3/home.asp
You can use the code below to redirect a subdomain and you will have to place the code as default document on the document root.
While applying the above code, be sure to replace the subdomain.HostingAccountDomain.com with your real subdomain URL, replace the /subdomain with the real name of your subdirectory and in the last piece, note that you will be redirecting to a given file.