how to match www urls with regular expressions - asp.net

I have found this bit of web.config code for checking the url to see if its missing the www. if it is then it redirects the user to the www. url
<rewrite>
<rules>
<clear />
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
however i need to do the opposite and i have been hacking around with it (im not very good with regular expressions, no matter how much i try) to try and get it to see if the url has www then redirect it to the non www
reason being is i have a subdomain
trade.words.co.uk
but i want to make sure they dont go to www.trade.words.co.uk
Thanks for any help in advance

Try this:
<rewrite>
<rules>
<clear />
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?:www\.)([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

I guess you just need the opposite web.config rule using an extended regular expression to match proper url like the following :
<rewrite>
<rules>
<clear />
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^([\w-]+://?|www[.])([^\s]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))$" />
</conditions>
<action type="Redirect" url="http://{R:2}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Hope this helps

Related

Setting up URL Rewrite rule

I've a rewrite role in my web.config, and it's redirect to mydomain.com/de.
But now I will replace the action de/{R:1} with the user language, where I can get from {HTTP_ACCEPT_LANGUAGE}. But this is a string of language and looks like fr-CH,**fr**;q=0.8,en;q=0.6,de;q=0.4,de-CH;q=0.2/
So it's there possibility to get the out only the **fr** of this string?
Thanks for any help.
This is my role:
<rule name="mydomain.com" stopProcessing="true" >
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="/en/|/de/" negate="true" />
</conditions>
<action type="Redirect" url="de/{R:1}" />
</rule>
In the past i have used the following rule to redirect to a language based page
<rewrite>
<rules>
<rule name="RedirectToLang" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{R:0}/{HTTP_ACCEPT_LANGUAGE}" appendQueryString="true" />
<conditions>
<add input="{HTTP_ACCEPT_LANGUAGE}" pattern=".+" />
</conditions>
</rule>
</rules>
</rewrite>

Site getting redirect to www.www.sitename.com

I am having this problem with my site getting redirect to www.www.sitename.com.
I have this code written in web.config which working fine for my other website.
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to WWW" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
This is 100% wokring code other sites but having problem of www.www.sitename.com on this site srilanka-tours.co
How about doing it less generic and more focused on your actual host name?
E.g.:
<rule name="Redirect to WWW">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.srilanka-tours\.co$" negate="true" />
</conditions>
<action type="Redirect"
url="http://www.srilanka-tours.co/{R:1}"
redirectType="Permanent" />
</rule>
(This is an excerpt from a real-world rewrite rule).
The above rule is similar to what you get when using the "Canonical Domain Name" in the IIS Managers' "wizard" for creating rewrite rules. See the article on Scott Guthrie's blog.

What's wrong with IIS7 rewrite rule

I'm trying to do a simple redirect from the www subdomain to the root domain. After trying many variations, I'm stuck and hoping for help. Feeling sure it's something stupid, but can't see it. Here's the rule I'm using.
<rewrite>
<rules>
<rule name="Redirect to root" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.stitchamerica.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://stitchamerica.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Input greatly appreciated.
This works for me:
<rewrite>
<rules>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://YOURSITE.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

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?
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>

Conditional query string redirect

I'm trying to redirect requests that have a query string to a different domain name.
I've got a short url, http://short.url and I want to redirect http://short.url?hello to http://long.url/?hello. So the query string has to be kept by the redirect. To make things more complicated, I would like to rediect http://short.url?hello,hello2 to http://long.url/advanced.aspx/?hello,hello2.
Here is the rule I've got now (only dealing with the first part of my question)
<rewrite>
<rules>
<rule name="test" patternSyntax="ECMAScript" stopProcessing="true">
<match url="~/?\w+" />
<action type="Redirect" url="http://long.url/?{R:0}" redirectType="Found" />
</rule>
</rules>
</rewrite>
However, I am not seeing any redirects. Also, is there a better way to do this? Basically I just want to setup a shortcut to pass queries to a website. These are not meant to be permanent so I'm using redirectType="Found".
In case anyone is looking to do this:
<rules>
<rule name="Basic" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\w*" />
<action type="Redirect" url="http://longurl.com" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="\w+" />
<add input="{QUERY_STRING}" pattern="\," negate="true" />
</conditions>
</rule>
<rule name="Advanced" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\w*" />
<action type="Redirect" url="http://longurl.com/advanced.aspx" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="^\w+(?=\,)" />
</conditions>
</rule>
</rules>

Resources