Url rewriting issue on azure - asp.net

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>

Related

ASP.net URL Rewrite subdirectory to external URL

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.

URL Rewrite (in Web.config) not redirecting

I'm trying to test URL Rewrite changing my Web.confing file.
I want to always redirect to /arquivos, as a test.
But nothing is happening.
Here's a part of my Web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="CrawlerBotRedirect" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="/arquivos" />
</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.

How to route ALL requests for domain to a sub folder asp.net

I have seen lots of solutions to this but none seem to work....
I have a domain
producerpte.co.uk
But I have a separate application set up in a sub folder so to see the root of that application you have to go to...
producerpte.co.uk/producerpte
However I want it so that the user does not see the 'site1' part of the url.
When I use url rewriting in Web.Server config or I use Context.RewritePath in code (in an application set up on the root) it simply redirects the user to the url (with site1 in it) I do not want the user to know they are going to a subfolder.
Am I setting this up wrong?
I'm actually using Winhost. All the examples ive tried just do not change result of the call without redirecting the url :-(
I have been told by winhost that I should do it with code/configuration. I would prefer to do it with code to be honest....
UPDATE : I tried Max's answer and I did...
<rules>
<rule name="Redirect if producerpte" stopProcessing="true">
<match url="^ producerpte/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder">
<match url="^.*$" />
<action type="Rewrite" url="producerpte/{R:0}" />
</rule>
</rules>
But no luck :-( And the urls still change in the browser....
If WinHost did enable IIS Rewrite Module, edit your web.config file and give this rule a try:
<rewrite>
<rules>
<rule name="Redirect if site1" stopProcessing="true">
<match url="^site1/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder">
<match url="^.*$" />
<action type="Rewrite" url="site1/{R:0}" />
</rule>
</rules>
</rewrite>
--- UPDATE ---
Well, I tested this IIS configuration:
I've added rules on the root site.
IIS created a web.config file:
Here's the content of the web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect if producerpte" stopProcessing="true">
<match url="^producerpte/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder">
<match url="^.*$" />
<action type="Rewrite" url="producerpte/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
http://localhost:8066/ displays the content of C:\inetpub\wwwroot\producerpte\producerpte without changing the URL in browser
http://localhost:8066/producerpte/ displays the content of C:\inetpub\wwwroot\producerpte\producerpte but the URL in browser is changed to http://localhost:8066/
Be sure that you created rules on the root site (and not the sub application) and I noticed that you had a leading space in your config: <match url="^ producerpte/(.*)$" /> between ^ and producerpte.

web.config rule for sub-directory to sub-domain redirection

I was already running a sub-website under following path
http://example.com/sub-site/
Now, I have created a sub-domain
http://sub-site.example.com/
Require "web.config" file code that can do 301 in following way
Request Redirect to
--------------------------------------------------------------------------
http://example.com/sub-site/ http://sub-site.example.com/
http://example.com/sub-site/page-1.html http://sub-site.example.com/page-1.html
http://example.com/sub-site/page-2.html http://sub-site.example.com/page-2.html
.
.
.
http://example.com/sub-site/page-N.html http://sub-site.example.com/page-N.html
Basically, rule should be written in such a way that "http://example.com/sub-site/" is changed to "http://sub-site.example.com/" in request, and browser should be redirected to newer location.
This should work.
<rewrite>
<rules>
<rule name="redirect to subdomain" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://sub-site./{R:1}" appendQueryString="false" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>

Resources