ASP.NET httpRedirect exactDestination for all pages except root - redirect

I'm changing the primary domain for an IIS website, and I need to keep existing URLs working. At the same time I am changing the behaviour of the start page of this (the previous) domain. This domain has previously been accessible at its root level, but now I need the root page (default document) to redirect to a specific subdirectory.
In summary: I need to, while redirecting, keep the trailing path for all pages except for the default document (root). Example:
www.old.com/ -> www.new.com/en
www.old.com/en/page2.htm -> www.new.com/en/page2.htm
www.old.com/en/page3.htm -> www.new.com/en/page3.htm
www.old.com/page1.htm -> www.new.com/page1.htm
I have this web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://www.new.com" httpResponseStatus="Permanent" exactDestination="false" />
</system.webServer>
<location path="Default.aspx">
<system.webServer>
<httpRedirect enabled="true" destination="http://www.new.com/en" httpResponseStatus="Permanent" exactDestination="true" />
</system.webServer>
</location>
</configuration>
I have set the IIS setting Default Document to Default.aspx.
But this doesn't work.

I solved it by using the URL Rewrite Module in IIS instead:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Root request" enabled="true" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="http://www.new.com/en" redirectType="Found" />
</rule>
<rule name="Path request" enabled="true" stopProcessing="true">
<match url="^(.*)$" />
<action type="Redirect" url="http://www.new.com/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

My IIS redirect rule is not working via web.config

I have a redirect rule that I am trying to create using web.config. I am trying to redirect
from the below page:
https://example.org/content/content.php?PrintMe=0&PgName=gothedistance
to this page:
https://example.org/gothedistance
My url redirect rule looks like the below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect" stopProcessing="true">
<match url="content.php$" />
<conditions>
<add input="{QUERY_STRING}" pattern="PrintMe=0&PgName=gothedistance" />
</conditions>
<action type="Redirect" url="gothedistance" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

How do I redirect a specific port in the IIS server to an other port

My URL Rewrite rule only works for port 80 in IIS .
The rewrite works for : http://localhost:80 --> http://localhost:8100
The rewrite works for : http://localhost:80/redirect --> http://localhost:8100
The rewrite doesnt work for : http://localhost:8080 --> http://localhost:8100
The rewrite doesnt work for : http://localhost:8080/redirect --> http://localhost:8100
My web.config is following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
<rewrite>
<rules>
<rule name="Redirect to port 8100" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT}" pattern="^8100$" negate="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}:8100/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The web.config is only working on default http port 80/443 but not for an specific port.
Which changes has to be made in the web.config for working with all ports?
The main problem is the variable {HTTP_HOST} which already contains the PORT information.
To solve this issue you need to adjust two things:
Extend the matching pattern within
Set the correct address for the redirect
I adjusted your web.config to see how it should work with your example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
<rewrite>
<rules>
<rule name="Redirect to port 8100" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" >
<add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
</conditions>
<action type="Redirect" url="http://{C:1}:8100/{R:0}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

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 Express Url Rewrite with 2 params don't work

Simply I have this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="index/page/method to index.php?page=page&method=method">
<match url="^index/([^/]+)/([^/]+)$" />
<action type="Rewrite" url="index.php?page={R:1}&method={R:2}" />
</rule>
<rule name="index/page to index.php?page=page">
<match url="^index/([^/]+)$" />
<action type="Rewrite" url="index.php?page={R:1}" />
</rule>
<rule name="index to index.php">
<match url="^index" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
When I call localhost I get the following:
HTTP-Fehler 500.19 - Internal Server Error
My problem is the first rule doesn't work :(
The rules work fine as I delete the first rule...
Can someone help me?
Edit by myself:
IIS doesn't like the &...so I changed it to & and it works fine^^
It is clear that you shoud use &, as Microsoft says so in its article,
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module