ASP.net URL Rewrite subdirectory to external URL - asp.net

I need to URL Rewrite a subdirectory to an external domain.
Example: When visiting "https://example1.com/test", "https://example2.com/hello" should open. The URL should still be "https://example1.com/test".
I tried to solve this by adding a Rewrite rule in web.config, but it don't works. Here is the Rewrite rule I made:
<rule name="TestRule" stopProcessing="true">
<match url="^test(/.*)?$" />
<action type="Rewrite" url="https://example2.com/hello" appendQueryString="false" />
</rule>

In order to redirect the incoming request to another domain by using Rewrite action type (stay the old URL in the browser address bar), we need to install Application Request Routing module.
https://www.iis.net/downloads/microsoft/application-request-routing
By default, Rewrite action only forwards these requests to the same domain, therefore, we only can specify a URL path in the Rewrite URL field.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#rewrite-action
Otherwise, Redirecting the incoming request to another domain will cause a 404 error.
After we installed the ARR extension, enable Reverse Proxy functionality following the below steps.
Here is an official example.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing
Finally, please refer to the below configuration.
<rewrite>
<rules>
<rule name="Myrules" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="/test.*" />
</conditions>
<action type="Rewrite" url="https://www.bing.com/maps" appendQueryString="false" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
It will redirect the requests that has “/test” segment to the https://www.bing.com/maps.
If we want to complete the task without installing ARR extension, we have to use Redirect action type. The URL in the browser address bar will change while we access the qualified URL.
<rewrite>
<rules>
<rule name="Myrules" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="/test.*" />
</conditions>
<action type="Redirect" url="https://www.bing.com/maps" appendQueryString="false" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
Feel free to let me know if there is anything I can help with.

Related

Url rewriting issue on azure

I have a subdomain, test1.test.com, i want to do a url rewriting on that subdomain to rewrite all requests to point other domain. For example i want to rewrite all request to test1.test.com to point http://www.AAA.com
here is my rewrite rule.
<rewrite>
<rules>
<rule name="Rewrite sample">
<match url="(.*)" />
<action type="Rewrite" url="http://www.AAA.com" />
</rule>
</rules>
</rewrite>
the result is not what i expected.
"The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
It's not possible to rewrite to another domain. Rewrite deals only with the last part of the url (after the domain) so the only way to change domain is to use redirect like this:
<rewrite>
<rules>
<rule name="Test" stopProcessing="true">
<match url="test1.test.com" />
<action type="Redirect" url="http://www.AAA.com"/>
</rule>
</rules>
</rewrite>

how to redirect from http to https if we explicitly give http in the url

I want to redirect an url from http to https if we explicitely give http in the url bar.
// I tried keeping the below code snippet in web.config
<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="Permanent" url="some url" />
</rule>
</rules>
</rewrite>
This is working fine when I tried like this in the url bar, for
example: sampledomain.com,
but this is not working when:
I explicitly give http://sampledomain.com in the url bar
I try to access directly home page of our site i.e sampledomain.com/Home
I want the solution for the above two methods.
Hope this link helps:
HTTP to HTTPS redirects on IIS 7.x and higher

Rewrite url with correct HTTP/HTTPS scheme

I have the following rule in my web.config file. As you can see it takes all subdomains and redirects to 'www.'
I also have an HTTP module that does switching between HTTP and HTTPS on the OnPreRequestHandlerExecute event. From what I have read and understood, this occurs after the rewrite but I could be wrong.
This code actually works in Firefox, transferring to www. then HTTPS where necessary, however it doesn't work in either Chrome or IE, maintaining the HTTP scheme.
I would like to make some changes to the code below so that the scheme is carried over rather than just assume HTTP which is does at the moment.
<rewrite xdt:Transform="Insert">
<rules>
<rule name="Redirect to www">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

URL rewrite rule as Subdomain not working

i'm using below rule to rewrite URL as subdomain.
<rewrite>
<rules>
<rule name="Redirect to Subdomains" stopProcessing="true">
<match url="^LandingPage.aspx$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^val=(.+)$" />
</conditions>
<action type="Rewrite" url="http://{C:1}.{HTTP_HOST}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
but it's not working. it's taking me to the Main domain.
I've added wild card DNS in my SOA hosting file. and have installed ARR on IIS with proxy mode enabled.
I'll be entering URL as http://www.example.com/LandingPage.aspx?val=somevalue
And this rule rewrite it as somevalue.example.com/. Still it's not working. Please correct me where I'm going wrong.

Configure redirect asp.net/iis7

I have a website that opens when the user enters either http://example.com or http://www.example.com.
I need to configure the server or the app to redirect with a permanent redirect to www.example.com when example.com is accessed.
What is very important is that the path is preserved, so if example.com/path/page.aspx?p=1, the redirect should be done to www.example.com/path/page.aspx?p=1.
thanks!
Using the URL Rewrite you can do this by adding a configuration in you web.config. You need to install this module in your IIS as well. Here is an example, not fully tested:
<system.webserver>
<rewrite>
<rules>
<rule name="Redirecting" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP}" pattern="^(http://)?example.com" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webserver>
The URL Rewrite module should do exactly what you need.

Resources