Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper method to remove www from address using IIS URL Rewrite

What is the optimal way to remove the www subdomain from a url using IIS URL Rewrite?

like image 598
Chris Marisic Avatar asked Sep 10 '11 00:09

Chris Marisic


People also ask

What is Request_uri in URL Rewrite?

Returns exact URL what you requested. For example, if you have default.aspx file in the root and you will access your website root.

How does IIS URL Rewrite work?

The concept of URL rewriting is simple. When a client sends a request to the Web server for a particular URL, the URL rewriting module analyzes the requested URL and changes it to a different URL on the same server.


2 Answers

If you want it to work with any hostname (not hardcoding it into the rule), you'd want to do something like this:

<rule name="Remove www" stopProcessing="true">   <match url="(.*)" ignoreCase="true" />   <conditions logicalGrouping="MatchAll">     <add input="{HTTP_HOST}" pattern="^www\.(.+)$" />   </conditions>   <action type="Redirect" url="http://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" /> </rule> 

in the redirect action, the {C:1} contains the second capturing group in the condition, whereas the {R:0} contains whatever was in the rule (the path). appendQueryString="true" will also append any querystring to the redirect (if present). Keep in mind though, that any url hashes, if present, will be lost in the process since those don't get passed to the server.

like image 82
Jani Hyytiäinen Avatar answered Sep 20 '22 23:09

Jani Hyytiäinen


IIS does it automatically for you:

Select site > URL rewrite > new rule > Canonical Host Name :)

like image 38
frapeti Avatar answered Sep 20 '22 23:09

frapeti