Redirect single page to another domain - asp.net

I need to redirect couple old webpages from my IIS to a new domain in a specific order.
For instance :
domain1/page1 to domain2/page1
domain1/page2 to domain2/page3
domain1/page3 to domain2/page5
...
Here's my webconfig <system.webserver> section:
<system.webServer>
<rewrite>
<rules>
<rule name="redirect single page" patternSyntax="ExactMatch" stopprocessing="true">
<match url="domain1/page1"/>
<action type="redirect" url="domain2/page1" appendquerystring="false"/>
</rule>
</rules>
</rewrite>
<httpredirect enabled ="true" destination"Domain2" httpresponsestatus="permanent"/>
</system.webServer>
But the redirecting is not happening it still points to the old domain pages.

Accordingly the #Claus answer on the question: 301 Redirect one domain to another using web.config
You can use the following code to achieve this:
<system.webServer>
<rewrite>
<rules>
<rule name="redirect" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www.domain1.com$" />
</conditions>
<action type="Redirect" url="http://www.domain2.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<system.webServer>
This example will match all URLs unless the hostname part is exactly www.domain1.com - and redirect those to www.domain2.com/whatever.

Related

Creating 301 redirects from asp.net website to another

I have a website which i'm planning to close down and replace with another with a different domain, how can i go about setting up 301 redirects from specific pages on my old site to the relevant pages on my new one?
I've had a look into the IIS Url Rewrite Module, but wasn't able to figure it out. Help please?
I've tried this
<rewrite>
<rules>
<rule name="Redirects to new domain" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="http://domain.com/" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
But nothing happens when navigation to the homepage
You can update your web.config with routing rules like this:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirects to new domain" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="olddomain.net" />
</conditions>
<action type="Redirect" url="http://www.newdomain.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>

IIS 7.5 Url Rewrite ignoring question mark

I'm trying to create a rewrite rule in IIS 7.5 Url Rewrite. What I'm trying to achieve is when someone clicks on a certain pdf file, I want them to be redirected to a form and then when they've filled out the form they get the pdf file which simply has a question mark.
e.g. ?download=true appended to the end of it that passes through the rewrite rule so:
pdf/my-pdf-file.pdf will be redirected to go-to-this-file.aspx (which would be a form)
then they're redirected to :
pdf/my-pdfpfile.pdf?download=true which the rewrite rule shouldn't pick up but it does which is my problem.
Here is my rule:
<system.webServer>
<rewrite>
<rules>
<rule name="My PDF Rule">
<match url="^pdf/my-pdf-file.pdf$" ignoreCase="true" />
<action type="Rewrite" url="/go-to-this-file.aspx" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
Please can someone help. Cheers!
The problem is simple, you should check if the {QUERY_STRING} contains download=true and then don't redirect. Try this:
<system.webServer>
<rewrite>
<rules>
<rule name="My PDF Rule">
<match url="^pdf/my-pdf-file.pdf$" ignoreCase="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="download=true" negate="true" />
</conditions>
<action type="Rewrite" url="/go-to-this-file.aspx" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>

web.config Index Redirect

I use a standard web.config file for my projects that are hosted on Windows servers. It takes care of 2 tasks.
It redirects the non-www version of the site to the www version.
It redirects the index file to the root.
See below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="index\.asp(?:l)?" />
<conditions>
<add input="{HTTP_HOST}" pattern="example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/" />
</rule>
<rule name="CanonicalHostNameRule2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This works great, except for 1 problem. The index redirect is redirecting all sub-directories to the main, index.
For example:
http://www.example.com/index.asp is redirecting to http://www.example.com like it should.
But, http://www.example.com/about/index.asp is also redirecting to http://www.example.com. I would like it to redirect to http://www.example.com/about/
Any help would be greatly appreciated.
Well, after getting no response here, I posted on various .Net forums and finally got an answer. This rule will fix it:
<rule name="redirect index.asp" stopProcessing="true">
<match url="^(\w*/)?index\.asp" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain\.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" />
</rule>

Redirect to www in IIS7 - classic pipeline mode

I want to implement redirects on an IIS7 webserver. Basically, if the subdomain is not included in the URL, I will redirect to the www subdomain.
http://mysite.com/file.aspx redirects to http://www.mysite.com/file.aspx
http://mysite.com/image.jpg redirects to http://www.mysite.com/image.jpg
http://mysite.com/text.html redirects to http://www.mysite.com/text.html
How to do this?
I do not want to write any HTTP Module -- it must be done thru IIS config only.
Also, I am required to use Classic Pipeline mode and cannot install any ISAPI plugins.
Is it possible?
You can throw this into your web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^http://mysite.com$" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
In IIS7 it can be done through the url rewrite section.
This solution worked for me:
1) Install URL Rewrite component:
http://www.iis.net/download/urlrewrite
2) Add to web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite\.com$" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>

IIS URL Rewrite and Web.config

I don't understand anything about IIS, but am trying to solve this problem of redirecting all visitors to example.com/page to example.com/page.html
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRedirects">
<add key="/page" value="/page.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration>
A couple of problems arise:
I don't know where to even put the file. There is a User root directory, and an htdocs directory, I tried both, no joy.
I don't even know if the account can do rewrites, I am trying to find that out.
1) Your existing web.config: you have declared rewrite map .. but have not created any rules that will use it. RewriteMap on its' own does absolutely nothing.
2) Below is how you can do it (it does not utilise rewrite maps -- rules only, which is fine for small amount of rewrites/redirects):
This rule will do SINGLE EXACT rewrite (internal redirect) /page to /page.html. URL in browser will remain unchanged.
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRewrite" stopProcessing="true">
<match url="^page$" />
<action type="Rewrite" url="/page.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
This rule #2 will do the same as above, but will do 301 redirect (Permanent Redirect) where URL will change in browser.
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^page$" />
<action type="Redirect" url="/page.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
Rule #3 will attempt to execute such rewrite for ANY URL if there are such file with .html extension (i.e. for /page it will check if /page.html exists, and if it does then rewrite occurs):
<system.webServer>
<rewrite>
<rules>
<rule name="DynamicRewrite" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}\.html" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/{R:1}.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
Just wanted to point out one thing missing in LazyOne's answer (I would have just commented under the answer but don't have enough rep)
In rule #2 for permanent redirect there is thing missing:
redirectType="Permanent"
So rule #2 should look like this:
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^page$" />
<action type="Redirect" url="/page.html" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Edit
For more information on how to use the URL Rewrite Module see this excellent documentation: URL Rewrite Module Configuration Reference
In response to #kneidels question from the comments; To match the url: topic.php?id=39 something like the following could be used:
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^topic.php$" />
<conditions logicalGrouping="MatchAll">
<add input="{QUERY_STRING}" pattern="(?:id)=(\d{2})" />
</conditions>
<action type="Redirect" url="/newpage/{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
This will match topic.php?id=ab where a is any number between 0-9 and b is also any number between 0-9.
It will then redirect to /newpage/xy where xy comes from the original url.
I have not tested this but it should work.
Just tried this rule, and it worked with GoDaddy hosting since they've already have the Microsoft URL Rewriting module installed for every IIS 7 account.
<rewrite>
<rules>
<rule name="enquiry" stopProcessing="true">
<match url="^enquiry$" />
<action type="Rewrite" url="/Enquiry.aspx" />
</rule>
</rules>
</rewrite>

Resources