I have problem with redirect.
I have domain like:
example.com
and a few subdomain like
application.example.com
documents.example.com
It's hard to make a wildcard binding, so i want to do URL redirect in Default Web Site ;)
If somebody make mistake in URL address like:
doduments.example.com - I want to redirect him to example.com
Here the case is more complicated, because i need do something similar, but for another domain:
example.pl
and subdomains
aplikacja.example.pl
dokumenty.example.pl
I did something like this ;)
<rule name="*.example.pl" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="example\.pl$" />
</conditions>
<action type="Redirect" url="http://example.pl/" />
</rule>
<rule name="*.example2.pl" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="example2\.pl$" />
</conditions>
<action type="Redirect" url="http://example2.pl/" />
</rule>
Far from perfection, but it works.
I won't be able to filter domain in match url - i don't know why ;(
Related
I have multiple domains bound to one site. For one domain, I would like to leave everything untouched by the Rewrite module. For the other domain, for example: www.example.com, I wish to allow every request down this path www.example.com/allowedpath yet for any other requests from www.example.com and below, return a 404.
From your description, I understand that you want to achieve requirement below.
Domain1.com -> Should not be affected by Rewrite rule
Example.com -> Example.com/allowed_path
Example.com/any_other_path -> Abort request
To achieve the above requirement, I have created 2 rewrite rules below.
<rules>
<clear />
<rule name="Rule-1" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="/test_path" />
</rule>
<rule name="Rule-2" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="example.com" />
<add input="{REQUEST_URI}" pattern="/test_path" negate="true" />
</conditions>
<action type="AbortRequest" />
</rule>
</rules>
Test result with Domain1.com
Test result with Example.com
Further, you could modify these rules as per your own requirement.
IIS 10 server behind an AWS application load balancer will not redirect traffic for domain without www when client requests http rather than https. The rule to redirect traffic when www is specified works fine, but 404 is returned if you try the same url without www.
So:
Enter "http://dname.com/blog" = 404
Enter "http://www.dname.com/blog" = redirect to "https://www.dname.com/blog"
<rule name="Force HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
<add input="{HTTP_HOST}" pattern="^dname\.com$" />
</conditions>
<action type="Rewrite" url="https://www.dname.com{REQUEST_URI}" />
</rule>
<rule name="Force WWW HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
<add input="{HTTP_HOST}" pattern="^www\.dname\.com$" />
</conditions>
<action type="Redirect" url="https://www.dname.com{REQUEST_URI}" />
</rule>
Nothing worked for me even after going through the answers provided on different forums.
After 2 days of banging my head in this here's what I found which solved the issue :
Bindings : Port 80 must be enabled (This can be added in bindings section in IIS).
SSL settings : Required SSL must be unchecked.
Add Rule :
<rewrite>
<rules>
<rule name="http to https redirection" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
appendQueryString="false" />
</rule>
</rules>
</rewrite>
Verify web config as it should reflect the rule added in IIS.
I don't know why the previously posted rules wouldn't work, but I was able to create a refined rule that is working:
<rule name="Force HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
<add input="{HTTP_HOST}" pattern="^(www\.)?dname\.com$" />
</conditions>
<action type="Redirect" url="https://www.dname.com{REQUEST_URI}" />
</rule>
The above rule combines the two rules instead of looking for the domain without the www and then with the www in a separate rule. The regex (www\.) tells the rule to look for "www." and the question mark tells it that it may or may not be there, so that includes the domain with and without the www.
There is a very very important step that should take care, before setup a redirect configure.
in web Sites project --> Actions(in the right) --> Bindings , the content will like below:
Binding Content
You take carefully the yellow color part, the yellow part is your original web IP address. This original IP address must exist in "Site Bindings", without the yellow part the URL redirect will not working anymore.
The following config is my current IIS URL redirect setting:
<rewrite>
<globalRules>
<rule name="Http redirect to Https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="localhost:8080" /> <-- the Red one should match above Site Bindings original IP address
</conditions>
<action type="Redirect" url="https://Your-Host-Name/{R:1}" redirectType="SeeOther" />
</rule>
</globalRules>
</rewrite>
Below I have a rule that works great with subdomains of example.com, but I need to use https://example.com (example.com) now. The {C:1} was always the subdomain, but now that I dont have a subdomain it needs to be "other". Is there a way to set a default if the {C:1} is null or empty to "other" and match on the host name without subdomain.
<rule name="Apple Icon Rewrite" stopProcessing="true">
<match url="^(apple-touch-icon.*?)(?:-precomposed)?(.png)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(.*).example.com$" />
</conditions>
<action type="Rewrite" url="/assets/sports/{C:1}/icons/{R:1}{R:2}" appendQueryString="false" />
</rule>
You can just add one additional rule, which will catch only exmple.com domain like that:
<rule name="Apple Icon Rewrite" stopProcessing="true">
<match url="^(apple-touch-icon.*?)(?:-precomposed)?(.png)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Rewrite" url="/assets/sports/other/icons/{R:1}{R:2}" appendQueryString="false" />
</rule>
I am using a web.config to redirect all non www to www which is working fine. How do I add a rule to ignore all subdomains so http://example.com redirects to http://www.example.com but any subdomain http://*.example.com or http://one.example.com do not redirect to www. (I cannot add the list of subdomains).
<rule name="Redirect to www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}" redirectType="Permanent"/>
</rule>
I think Joey's answer only covers the cases where domains are ending in .com and can't handle cases like .co.uk .co.za .com.au etc. You can extend the regular expression a little more but allowing for different types of domains using [a-z] will end up redirecting the subdomains as well, which is what you don't want according to the question.
So in order to achieve this, you'll have to handle the specific cases that you think can arise in your situation.
<rule name="Redirect to www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^([a-z-]+[.](com|co|ca|net)([.](uk|sg|au)){0,1})$" negate="false" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}" redirectType="Permanent"/>
</rule>
You can modify the regular expression to your liking.
You could try to with the following code:
<rules>
<rule name="Redirect to www">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
<add input="{HTTP_HOST}" pattern="^one\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}"/>
</rule>
</rules>
I just added a dotmobi mobile site from Network Solutions to complement my full site. I was able to successfully redirect smartphones (but not iPads) to the site with these two URL rewrites on my IIS server:
<rule name="RequestBlockingRule2" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="ipad" />
</conditions>
<action type="None" />
</rule>
<rule name="RequestBlockingRule1" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone|android|iphone" />
</conditions>
<action type="Redirect" url="http://www.wkmclaughlin.mobi" />
</rule>
My problem now: How do I bypass redirection on requests from the mobile site itself to the main site? I've been playing around with {HTTP_REFERER} for hours and getting nowhere.
p.s. The mobile site only lets you specify the root of your domain -- you can't specify a page as a flag to url rewrite.