I have this rule for redirecting traffic to /public, but I get "redirect loop" error. Any suggestions?
<rule name="Redirect to /public" stopProcessing="true">
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^test.example\.com$" ignoreCase="false" />
</conditions>
<action type="Redirect" url="http://test.example.com/{R:1}" />
<match url="(.*)" ignoreCase="false" />
</rule>
You have a loop because your rule is triggered when:
url="(.*)" => always true
{HTTP_HOST} matches ^test.example\.com$ => always true in your case
Your redirect rule is triggered by any url accessing your website.
It then redirects to your website with an url that will match again the rule...
If you want to redirect all the requests to /public, you can use the following rule:
<rule name="Redirect to /public" stopProcessing="true">
<match url="^public/(.*)" negate="true" />
<action type="Redirect" url="public/{R:1}" />
</rule>
It checks if the url doesn't start with public/. If it is the case, it redirects to public/UrlRequested.
You can keep your conditions as well:
<rule name="Redirect to /public" stopProcessing="true">
<match url="^public/(.*)" negate="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^test.example\.com$" ignoreCase="false" />
</conditions>
<action type="Redirect" url="public/{R:1}" />
</rule>
Related
I'm using a HTTP to HTTPS redirect in my MVC application.
This is the code that I'm using 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="Found" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
There is a page in my application that doesn't work on HTTPS, so I need to add an exception to this rule.
I was wondering if anyone can help me on that or show me some tutorials/examples.
You just need to add another condition that negates the rule for your url. Your new rule would be something like this:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{R:1}" pattern="myfolder\/myfile\.aspx" negate="true"/>
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
Given this URL:
http://domain1.com/pdfs/file.pdf
How can I redirect domain1 to domain2 using the pdfs/* pattern?
I am trying to redirect http://domain1.com/pdfs/file.pdf to http://domain2.com/pdfs/file.pdf
I have tried:
<rule name="Publications" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{URL}" pattern="^pdfs/$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://domain2.com/pdfs/{R:0}" />
</rule>
Thanks
I found it.
<rule name="Publications" enabled="true" stopProcessing="true">
<match url="^pdfs/(.*)" ignoreCase="true" />
<action type="Redirect" url="http://domain2.com/pdfs/{R:1}" />
</rule>
I am trying to rewrite/redirect a url in ASP.NET 4.0
Example:
Requested URL:
http://www.domain.com/accounting/blog/post/2009/07/17/Getting-the-most-out-of-your-account-firm.aspx
Rewritten/Redirected URL:
http://blog.domain.com/post/2009/07/17/Getting-the-most-out-of-your-outplacement-firm.aspx
In other words, change the sub-domain from "www" to "blog", and remove "/accounting/blog" from the directory structure, then redirect.
Here is the rule I am using (passes pattern matching tests in IIS, but does not work):
<rewrite>
<rules>
<rule name="blog redirect" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="www\.domain\.com\/accounting\/blog\/(.*)" />
</conditions>
<action type="Redirect" url="http://blog.domain.com/{C:1}" />
</rule>
</rules></rewrite>
Any help would be greatly appreciated.
This will work for any domain:
<rules>
<rule name="blog redirect" stopProcessing="true">
<match url="^accounting/blog/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^www\.(.*)" />
</conditions>
<action type="Redirect" url="http://blog.{C:1}/{R:1}" />
</rule>
</rules>
I have added this rule in my web.config to redirect non www URLs to www.
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
Although it is working fine for main site URL. Like if user types http://example.com it redirects to http://www.example.com
But for some URLs like
http://www.example.com/userfiles/abc.pdf
it redirects to
http://www.www.example.com/userfiles/abc.pdf
Here you can see 2 time www in URL.
I guess this one should work:
<rule name="Redirect to WWW" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
If this one does not work as expected, you could try to add another condition:
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
Hope this helps.
I need to redirect several host headers without www. to their www. counterpart. I can't seem to get it working quite right. This is what I've got so far:
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." negate="true" />
</conditions>
<action type="Redirect" url="http://www.{C:0}/{R:0}" redirectType="Permanent" />
</rule>
The domains are all totally different, so there is no common string to match except for .com.
My regex is probably not right...
Try this:
<rule name="Redirect to WWW" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\..*" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
EDIT: fixed regex, should work now.