Proper method to remove www from address using IIS URL Rewrite - iis-7

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

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.

IIS does it automatically for you:
Select site > URL rewrite > new rule > Canonical Host Name :)

The following one should work :
<system.webServer>
<rewrite>
<rules>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://www.example.com{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

To do a redirect that will work for both http and https the following can be used
<rewrite>
<rules>
<rule name="Lose the www" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true"/>
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.*)$"/>
</conditions>
<action type="Redirect" redirectType="Permanent" url="{SchemeMap:{HTTPS}}://{C:1}/{R:1}" appendQueryString="true" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="SchemeMap">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>

Related

url redirection in IIS not working for non existing pages

I had a page http://domain1.com/blog.aspx.
This page I have deleted and created a website with a new domain just for this page.
like below http://domain2.com/blog.aspx
Then I have added a rule in domain1.com web.config like below
<rewrite>
<rules>
<rule name="Redirect blog" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain1.com/blog.aspx$" />
</conditions>
<action type="Redirect" url="http://domain2.com/blog.aspx" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But I get 404 error while visiting http://domain1.com/blogpage.aspx
How can I fix this issue?
A few issues...
{HTTP_HOST} = The host name which is domain1.com, so it will never match your pattern
Also you can do this pattern="^domain1.com/blog.aspx$" the . need to be escaped.
Now you have to match also on the request_uri to capture the page. Below should work. Providing you are re-directing domain1.com/blogpage.aspx to domain2.com/blog.aspx
<rule name="Redirect blog" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" >
<add input="{HTTP_HOST}" pattern="^domain1\.com$" />
<add input="{REQUEST_URI}" pattern="blogpage\.aspx$" />
</conditions>
<action type="Redirect" url="http://domain2.com/blog.aspx" redirectType="Permanent" />
</rule>
</rules>

Web.config URL Rewrites - HTTPS and Non-WWW

I need to have both https and non-www rewrites, while also NOT HARDCODING the domain, since we have numerous servers. This needs to be in the web.config, not in IIS.
I've read numerous articles:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
http://madskristensen.net/post/url-rewrite-and-the-www-subdomain
how to set asp.net web.config rewrite http to https and www to non-www
The https rewrite works, the non-www does not.
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<!--<add input="{CACHE_URL}" pattern="*://www.*" />-->
<!--<add input="{HTTP_HOST}" pattern="*://www.*" />-->
<add input="{HTTP_HOST}" pattern="^.*www.*" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
// i've also tried
// url="{C:2}/{R:1}"
// url="{C:1}/{C:2}"
</rule>
I tested the regex for ^.*www.* on a regex tester and it was matching www.testing.com but not testing.com - so I would assume the pattern would catch it.
I need the URLs to redirect from:
testing.com ---> https://testing.com
www.testing.com ---> https://testing.com
www.testing.com/xyz/ ---> https://testing.com/xyz/
Was my own issue - there was no DNS for the www, therefore the redirect wouldn't resolve on it's own.
Code used:
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
</rule>
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{CACHE_URL}" pattern="*://www.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>
Add a "Canonical domain name" rule.
The soulution Rob found works fine as well. Mine seems to be a bit easier IMHO.

http to https in IIS

I realize this is a simple question, but I'm just not finding the answer. I've applied the below rule...
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
The url I'm interested in has the pattern
http://[domain]/[directory]/[aspx page]
So http://example.com/funstuff/thefair.aspx
The result of the rewrite is http://[domain]/[an_aspx_page]
So the rewrite is removing the directory.
I assumed the {R:1} specifies a parameter that will be rewritten, and I tried https://{HTTP_HOST}/{R:1}/{R:2}, but this result in a 500 error.
I want to direct all traffic on this domain to https without changing the rest of the url entered by the user.
Here's what we use on one of our sites to auto-redirect all traffic to the https version. It also includes everything else in the url.
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>

WebConfig URL Rewrites in IIS

Have a ASP.NET solution and want to achieve 2 things: -
Redirect www.mydomain.com to mydomain.com
Have friendly URLs
So ... here is what I have added the system.webserver section of the webconfig...
<rewrite>
<rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.mydomain\.com" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://mydomain.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="homepage" stopProcessing="true" >
<match url="^/wwwzone/homepage.aspx$"/>
<action type="Redirect" url="/"/>
</rule>
<rule name="homepageReal" stopProcessing="true" >
<match url="^/$"/>
<action type="Rewrite" url="/somepath/homepage.aspx"/>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https"/>
<add key="off" value="http"/>
</rewriteMap>
</rewriteMaps>
</rewrite>
This fails. The logic behind this is: -
First rule takes care of redirecting www.mydomain.com to mydomain.com. This works. and with the rewriteMap it also handles HTTP/HTTPS correctly.
The second rule is supposed to do a browser redirect if they request an unfriendly (real) URL.
The last one is designed to convert the friendly URL back to the real URL, however this is a rewrite not a redirect.
Any thoughts much appreciated.
It turns our was poor syntax in my regular expressions.
So, matching the real URL of the home page should have been
<match url="^$"/>
not
<match url="^/$"/>
And the subsequent rule to rewrite to the real URL should change in the same way.
So, for completeness, this one works...
<rewrite>
<rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.mydomain\.com" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://mydomain.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="homepage" stopProcessing="true" >
<match url="^wwwzone/homepage.aspx"/>
<action type="Redirect" url="/"/>
</rule>
<rule name="homepageReal" stopProcessing="true" >
<match url="^$"/>
<action type="Rewrite" url="/somepath/homepage.aspx"/>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https"/>
<add key="off" value="http"/>
</rewriteMap>
</rewriteMaps>
</rewrite>

remove .aspx extention using IIS7 & URL Rewrite [duplicate]

I'm using ASP .NET rewriteModule to rewrite http://example.com to http://www.example.com.
<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
Then i have this inside <system.webServer>.
<rewrite>
<rules>
<rule name="Canonical" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^([a-z]+[.]com)$"/>
</conditions>
<action type="Redirect" url="http://www.{C:0}/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
Now i want to remove all the .aspx in the end of my pages. Example:
http://www.example.com/Register.aspx
Will turn into:
http://www.example.com/Register/
How can i do that?
I'm on Shared Web Hosting on GoDaddy using IIS7.
These are the standard rewrite rules I start every project with. I use only clean URLs for all the pages (example first rule works for www.example.com/about and second rule www.example.com/product/123)
<rewrite>
<rules>
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx" />
</rule>
<rule name="Rewrite page to aspx" stopProcessing="true">
<match url="^([a-z0-9/]+)$" ignoreCase="false" />
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
Pages where I need to parse out the ID (this case number only) and add it to the query string I add a similar rule to the front:
<rule name="Rewrite Product ID" stopProcessing="true">
<match url="^product/([0-9]+)$" ignoreCase="false"/>
<action type="Rewrite" url="product.aspx?id={R:1}"/>
</rule>
If you want to use lower and upper case letters in the URL, set ignoreCase="true"
Edit to answer your second question plus a bonus
This rule will redirect aspx page to the clean URL:
<rule name="Redirect to clean URL" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
<action type="Redirect" url="{R:1}"/>
</rule>
Replace url="{R:1}" with url="{ToLower:{R:1}}" to change URL to lowercase. See below why you would want to do this.
Also a good idea to update the Form action so that post backs don't return back to the ugly URL. Using IIS 7.5 or newer this should work:
if (!String.IsNullOrEmpty(Request.RawUrl))
form1.Action = Request.RawUrl;
or for IIS 7:
if (!String.IsNullOrEmpty(Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
form1.Action = Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
One more thing to keep in mind... it's a good idea to keep all URLs lower case. Mixing lower/upper case characters in the URL creates duplicate content issues for SEO/Google. For example website.com/About and website.com/about will load the same page, but Google will index them as two separate pages.
First you need to remove the .aspx (default.aspx) and redirect to default to change the browser address then add the .aspx and rewire to page using IIS
<rewrite>
<rules>
<clear />
<rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="RewriteASPX" enabled="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
<rewrite>
<rules>
<remove name="RewriteUserFriendlyURL1" />
<remove name="RedirectUserFriendlyURL1" />
<rule name="RedirectUserFriendlyURL2" stopProcessing="true">
<match url="^www\.myserver\.com/(.*)\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
</conditions>
<action type="Redirect" url="www.myserver.com/{R:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^www\.myserver\.com/(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="www.myserver.com/{R:1}.aspx" />
</rule>
</rules>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
<rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*)www\.myserver\.com/(.*)\.aspx$" />
<action type="Rewrite" value="www.myserver.com/{R:1}" />
</rule>
</outboundRules>
</rewrite>
this will do it - I have generated this vis IIS on my local machine - change myserver.com to your own URL. you can change the regex to actually take care of the x.aspx part of the url then it should work across all pages

Resources