Conditional query string redirect - asp.net

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>

Related

Http to Https URL rewriting

My requirement is straight forward. i have Http://abc need to make it Https://abc . I have added the folowing code in web.config. i.e. added new rule in IIS.
I have folowwed the URL rewriting module in IIS.
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^off$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
But still it doesn't work for me. Help me out.
The code you are using is very similar to what I use in production except that I use redirectType="Permanent" and I happen to use a hard coded domain name to ensure a canonical domain and I use {R:1} for the path and query but I think using {HTTP_HOST}{REQUEST_URI} as you are should work however you may need a slash between the two.
Give this a try:
<system.webServer>
<rewrite>
<rules>
<rule name="http 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>
</rules>
</rewrite>
</system.webServer>

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>

IIS/ASP Redirect a domain to another, both having the same source

I have to redirect a domain to another, tricky part being the new domain uses the source of the old one as a host. Implicitly, if I did a general redirect, it would loop and 500 Internal Error.
Grabbed my .htaccess guns to get ready for war only to find that the server is not linux and it uses asp. I've read a bit, accessed the web.config file and the default ruleset is this (which I assume is pretty much standard-issue)
<rewrite>
<rules>
<rule name="Default Document" stopProcessing="true">
<match url="(.*)default" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Is there any way I can do a conditional that checks the domain name and only if the rule matches, it redirects? Something like:
<rules>
<rule name="Redirect" stopProcessing="true">
<match url="(.*)oldDomain.com" />
<action type="Redirect" url="(.*)newdomain.com$" redirectType="Permanent"/>
</rule>
</rules>
// Actual domain and testing
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="(.*)intermedcasasibiu\.ro$" />
</conditions>
<action type="Redirect" url="{C:1}intermedcasasibiu.ro/{R:1}" redirectType="Permanent" />
</rule>
Which outputs :
http://intermedcasa.ro/intermedcasasibiu.ro/intermedcasasibiu.ro/intermedcasasibiu.ro/intermedcasasibiu.ro/intermedcasasibiu.ro/AndSoOn
Yes, it's possible:
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="(.*)newdomain\.com$" />
</conditions>
<action type="Redirect" url="{C:1}newdomain.com/{R:1}" redirectType="Permanent" />
</rule>
Apart from that, If you are more familiar with htaccess rewrite rules, you can import them into IIS, check this guide

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

how to match www urls with regular expressions

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

Resources