URL rewrite rule as Subdomain not working - iis-7

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.

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.

Translating apache rewrite rules to IIS web.config

I am trying to translate this apache rewrite rule into web.config rules but I can't get it to work.
Basically it checks the user agent and redirect the agent to the url provided
# allow social media crawlers to work by redirecting them to a server-rendered static version on the page
RewriteCond %{HTTP_USER_AGENT (facebookexternalhit/[09]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule qs/(\d*)$ http://sitetocrawl.com/doc?id=$1 [P]
This is what I have so far. However, I can't figure out how to catch the url querystring parameter. Basically the text string after http://example.com/qs/parameter
<rule name="Social Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="urltomatchpattern" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet" />
</conditions>
<action type="Redirect" url="http://sitetocrawl.com/doc?parameter" appendQueryString="true" redirectType="Found" />
</rule>
EDIT:
I tried with many variants of simpler rules, like redirect/rewrite when a specific user agent requests the site(in my case, the facebook crawler). But I can't even get those rules to work. I am debugging using the Facebook OG debugger
<rule name="Rule1" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="facebookexternalhit/1.1|Facebot" />
</conditions>
<action type="Redirect" url="new url here" />
</rule>
Not an answer but a starting point. The IIS Manager (IIS 8 on Windows 8.1) translates your apache mod_rewrite rules into this slightly different configuration:
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="qs/(\d*)$" ignoreCase="false" />
<conditions>
<add input="%{HTTP_USER_AGENT}" pattern="(facebookexternalhit/[09]|Twitterbot|Pinterest|Google.*snippet)" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="http://sitetocrawl.com/doc?id={R:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I see that it is rewrite instead of redirect, but please check if this would work for your scenario. And if it works, you may begin changing it until reaching desired result.
And now I see that Your main URL Matching pattern is simply urlmatchpattern which of course is not a pattern and is the root cause for your rules to not work.

Microsoft rewriting module - Force www on URL Or remove www from URL

I have a shared hosting plan with Windows Server 2008 and IIS7.5, and there is Microsoft rewriting module installed and enabled.
<rewrite>
<rules>
<rule name="myRule" patternSyntax="Wildcard">
<!--Rewriting code-->
</rule>
</rules>
</rewrite>
So, how to redirect mydomain.example/everywhere-in-site/my-page.html to www.mydomain.example/everywhere-in-site/my-page.html with Microsoft rewriting module?
And what if I want to redirect www.mydomain.example/everywhere-in-site/my-page.html to mydomain.example/everywhere-in-site/my-page.html?
To remove the www from a domain and redirect to a "naked domain" you could di it like in the following code snippet:
<rewrite>
<rules>
<rule name="Remove WWW prefix" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.yourdomain\.example$" />
</conditions>
<action type="Redirect" url="http://yourdomain.example/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
And the other way around (if you prefer that) to redirect a non-www to one with www:
<rewrite>
<rules>
<rule name="Add WWW prefix" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yourdomain\.example$" />
</conditions>
<action type="Redirect" url="http://www.yourdomain.example/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The redirectType="Permanent" is of course optional but for SEO and most scenarios I would recommend it.
Please see also these Stack Overflow questions/answers:
IIS7 URL Rewrite - Add "www" prefix
Forwarding http://mydomain.example/ctrlr/act/val to http://WWW.mydomain.example/ctrlr/act/val
Proper method to remove www from address using IIS URL Rewrite

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>

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