redirect subdomain to another domain - redirect

Current i am using windows shared hosting. I need to redirect my sub domain to a new domain(including the entire path).
For example, when user visit to http://oldsubdomain.olddomain.com/page1/topic1 , it will redirect user to http://newdomain.com/page1/topic1
This is the method suggest by my service provider
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
}
</script>
But this way seems like not able to redirect the entire url. Any suggestions?
Thanks
*UPDATE
Trying to redirect using web.config as below, but still get 500 Internal error, any idea?
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rule name="CName to URL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.olddomain\.com$" />
</conditions>
<action type="Redirect" url="http://newdomain.com/{R:0}" />
</rule>
</rewrite>
</system.webServer>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
</configuration>

Why not use the redirect method?
private void Page_Load(object sender, System.EventArgs e)
{
response.redirect("http://newdomain.com/page1/topic1");
}

Related

IIS: Redirect multiple URL's to same website

I'm looking for the best way to redirect multiple URL's to one new URL.
I have the following URLS:
https://domain.app.company.com/application
https://application.app.company.com
And I want those to redirect to: https://application.domain.app.company.com
What would be the best way to go about this (in IIS)?
I have tried using URL Rewriting, but I can only make that work for the first URL (https://domain.app.company.com/application):
<rule name="Application 1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*application" />
<action type="Redirect" url="https://application.domain.app.company.com" appendQueryString="false" />
</rule>
For the second URL (https://application.app.company.com), I can make it work when setting up a new (empty) website in IIS (that listens to said URL) and add a httpRedirect in it.
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="https://application.domain.app.company.com" exactDestination="true" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>
Is this the recommended way to go about this? Or would there be another way?
Thank you
Schoof
If you want to bind two url for the same IIS web site, I suggest you could try to below url rewrite. We could create two url rewrite rule to achieve your requirement.
<rule name="Application 1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*application" />
<action type="Redirect" url="https://application.domain.app.company.com" appendQueryString="false" />
</rule>
<rule name="test" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="application.app.company.com" />
</conditions>
<action type="Redirect" url="https://application.domain.app.company.com" />
</rule>

Rewrite subfolder only (HTTP to HTTPS) redirects to site root

I'm trying to rewrite HTTP to HTTPS for a specific subfolder on my IIS 8.5 web server but it's not working. I've read countless other solutions and blog postings but nothing I've tried works.
http://domain.example.com/one/two/three/
should redirect to... (same url but using https)
https://domain.example.com/one/two/three/
but instead is redirecting to... (site root using https)
https://domain.example.com
loading... (desired url with https)
https://domain.example.com/one/two/three/
also redirects to... (site root using https)
https://domain.example.com
it's removing the subfolders from the url.
This folder needs to also be protected with Windows Authentication, which I can get to work but the https redirection is failing with or without the authentication enabled so I don't think that's the cause.
Within IIS I selected the desired subfolder (/three/ in the example above) and created the Rewrite rule there.
<rewrite>
<rules>
<clear />
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
This should of course work with any files and folders contained within the desired subfolder. (/three)
I tried this and it redirects to the apparent correct url but gives the "too many redirects" error:
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="SeeOther" />
</rule>
You should avoid doing this:
Within IIS I selected the desired subfolder (/three/ in the example
above) and created the Rewrite rule there.
Instead setup the rewrite rules in Web.config at the application root. You can redirect a specific folder to HTTPS by including it in the match parameter as follows:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect Subfolder" stopProcessing="true">
<match url="^one/two/three/" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Please note that this is a minimal Web.config file that does what you are looking for. If your application already contains a Web.config in the root folder, then you will have to merge the above into your solution.

Redirect old URL with params to new URL with totally new params in web.config

I've been looking around for over two days now but I can't get this simple task to work in web.config:
Redirect a full URL with params to a full NEW URL with other params.
I.e.:
Redirect this:
h--p://www.somedomain.net/index.php?some=examples&someother=examples
to this
h--p://www.someotherdomain.net/blabla.php?all=kind&off=new&variables=ok
This is the closest I could get:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rule01" patternSyntax="Wildcard" stopProcessing="true">
<match url="http://www.somedomain.net/index.php?sid=73742&lang=nl" />
<action type="Redirect" url="http://www.someotherdomain.net/survey/index.php?r=survey/index&sid=73742&lang=nl" appendQueryString="false" />
<serverVariables>
</serverVariables>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But that redirects me from:
h--p://www.somedomain.net/index.php?sid=73742&lang=nl
to:
h--p://www.someotherdomain.net/survey/?sid=73742&lang=nl
where it should be:
h--p://www.someotherdomain.net/survey/index.php?r=survey/index&sid=73742&lang=nl
I hope the above is clear and someone can help me out, it's driving me nuts that i can't figure this out! :S

IIS default website conditional redirect upon domain name

I have many websites hosted on the same IIS. When I stop one of them, and trying accessing it, it renders the default website of IIS. i.e iisstart.htm.
What I need to have is, for example, when example1.com has been stopped and trying to access its home page or better any URL on it i.e example1.com/some/path it redirects to an other defined page on the root of the default website, such as, example1.html. Also, I need the solution to be applied as many as websites that I have, i.e example2.com, example3.com and so on.
I have tried the following web.config settings, but it does not work:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="conditional products redirect" enabled="true">
<match url="^example1(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="/example1.html" />
</conditions>
<action type="Redirect" url="/example1.html" appendQueryString="false" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

IIS 7.5 HTTP to HTTPS redirect

I am trying to have all HTTP requests redirect to HTTPS.
Here is my web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers accessPolicy="Read, Execute, Script" />
</system.webServer>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I found this sample in another post on Stackoverflow:
How to force HTTPS using a web.config file
When I save that file and try to access my site using http it does not redirect to https.
I thought that maybe the file was being ignored so I typed some incorrect syntax into the web.config then I got a 500 error- meaning that it is indeed looking at the web.config file.
Am I misunderstanding what the above configuration is supposed to do? I want to redirect all HTTP request to HTTPS.
The site that is being redirected to is a virtual directory for Tomcat if it makes a difference.
Assuming you have URL Rewrite Installed. Click here for info/installation.
Make sure you have the following configured within the URL Redirect in the IIS Manager.
Match URL Section
Requested URL: Matches the Pattern
Using: Regular Expressions
Pattern: (.*)
Make sure Ignore Case is checked
Conditions Section
Logical Grouping: Match All
Input: {HTTPS}
Type: Matches the Pattern
Pattern: ^OFF$
Action Section
Action type: Redirect
Action Properties
Redirect URL: https://{HTTP_HOST}/{R:1}
Make Sure 'Append Query String' is checked
Redirect Type: See other (303)
Just Try this:
<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="SeeOther" />
</rule>