IIS rewrite rule is not working in Azure production - azure

I have website with multiple domains and i want them to have different robots.txt
I have rule for this so depends on domain request it will return different robots. This is workin beta slot but after deploy to production slot its not working.
<rule name="robots" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="^robots.txt" />
</conditions>
<action type="Rewrite" url="https://{HTTP_HOST}/{HTTP_HOST}.robots.txt" />
</rule>
If i am changing action type to redirect it seems to work because webbrowser redirects and is in stuck with to many redirects (Which make sense.)

As url-rewrite-module-configuration-reference mentioned that the server variable REQUEST_URI can be used to access the entire requested URL path, including the query string. Assuming that the URL for robots.txt is https://<your-hostname-and-port>/robots.txt, then you would get the string /robots.txt as an input via REQUEST_URI.
<rewrite>
<rules>
<rule name="robots" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" ignoreCase="true"/>
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="^/robots.txt" />
</conditions>
<action type="Rewrite" url="{HTTP_HOST}.robots.txt"/>
</rule>
</rules>
</rewrite>
With the rule above, I could rewrite my URL and get the following result:
Changing the type of the rule action from Rewrite to Redirect, I could get the following result:

Related

iis url rewrite not redirecting from default site to defaultsite/app

I am having trouble wrestling url rewrite and need to handle this in IIS. What I need is to ensure that all traffic that hits the server is redirected to https://server/app. I can handle this using a standard http to https rewrite which seems to work fine redirecting http://server to https://server/app but it doesn't work if someone hits https://server.
Here's my standard rewrite:
<rewrite>
<rules>
<rule name="http to https" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/app" />
</rule>
</rules>
</rewrite>

How to add rules in Url redirect in iis 10

I want to write redirect rules in IIS 10. I googled it and spent half of my day figuring out but no luck so I am posting it to get some solution.
https://testing.app.com/apptest should redirect to https://testing.app.com/apptest/account/login
https://testing.app.com/apptest/ should redirect to https://testing.app.com/apptest/account/login
https://test-apptest.testing.app.com/ should redirect to https://test-apptest.testing.app.com/account/login
https://test-apptest.testing.app.com should redirect to https://test-apptest.testing.app.com/account/login
But when user types url https://testing.app.com/apptest/account/login or https://test-apptest.testing.app.com/account/login then it should not redirect anywhere and it should stay as it is.
<rewrite>
<rules>
<rule name="Test1" stopProcessing="true">
<match url="account/login" />
<action type="None" />
</rule>
<rule name="Test2">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="testing.app.com" />
<add input="{REQUEST_URI}" pattern="^/apptest" />
</conditions>
<action type="Redirect" url="https://testing.app.com/apptest/account/login" appendQueryString="false" />
</rule>
</rules>
</rewrite>
We just add an anchor point to the regular expression so that precisely matches the segment ‘/apptest’.
Updated
<system.webServer>
<rewrite>
<rules>
<rule name="MyRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^((/apptest)?)/?$" />
</conditions>
<action type="Redirect" url="https://{http_host}{C:1}/account/login" />
</rule>
</rules>
</rewrite>
</system.webServer>
Explanation
Since the hostname changed and will subsequently be appended in the redirection URL, I replace it with {http_host} server variable to follow it. Besides, {Request_URI} will return the URL path and {C:1} will return either "/apptest" or "". therefore I append it into the redirection URL.
The meaning of every server variable.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule
Finally, please don’t forget to install the URL Rewrite extension before applying the rules.
https://www.iis.net/downloads/microsoft/url-rewrite
Here is a quick reference of the regular expression from Microsoft documentation.
https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
Feel free to let me know if the problem persists.

URL rewrite Pattern in iis 10

I'm using Asp.Net web forms.In my URL I don't want to expose after the character '?'. I want the regular expression pattern to achieve this using IIS 10. I have tried this "Security(.+)$" but it doesn't work.
From this: www.some.com/Security/login.aspx?name=dfdf
To this: www.some.com/Security/login.aspx
If you don't want to expose query string.
1.you should redirect any request to www.some.com/Security/login.aspx?name=dfdf to www.some.com/Security/login.aspx.
2.Then you have to rewrite request from www.some.com/Security/login.aspx back to www.some.com/Security/login.aspx?name=dfdf
Just keep in mind that, this rule can only rewrite back to static query string ?name=dfdf.
If you need to need to dynamic rewrite the URL, then you may need to add a custom request header to save the query string. So that backend server will know where should it rewrite back.
<rules>
<rule name="redirect rule" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/Security/login.aspx\?(.+)" />
</conditions>
<action type="Redirect" url="Security/login.aspx" appendQueryString="false" redirectType="Temporary" />
</rule>
<rule name="rewrite back" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/Security/login.aspx$" />
</conditions>
<action type="Rewrite" url="Security/login.asp?name=dfdf" appendQueryString="false" />
</rule>
</rules>

Infinite Redirect Loop in IIS 7.5

I am trying to add redirect rule 'www' when the request comes without the 'www' for the site. ie. http://example.com to http://www.example.com
Here is what the rule looks like:
<rewrite>
<rules>
<rule name="Add www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com" />
</rule>
</rules>
</rewrite>
Once i add this rule, the site goes to infinite loop and it just error out (page can't be displayed error message). The server is brand new and it might be missing some redirect components (if there is one). I did install URL Rewrite component and added rule in it.
Any suggestions?
Thanks.
You have not chosen a pattern syntax and so you using the default regular expression syntax. As a result your pattern matches both example.com and www.example.com and causes an infinite loop. Try this:
<rewrite>
<rules>
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>

IIS7.5 URL Rewrite rule to perform a 301 redirect from mysite.hosting.com to mysite.co.uk

I use IIS 7.5
I have a website wich has a valid host like:
A) mysite.co.uk
and a DEFAULT host (using for testing proposes provided by the hosting company):
B) mysite.hosting.com
Website is visible on both address, creating a DUPLICATE CONTENT issue for Search Engine.
I need redirect all the traffic (for all pages) from B to A using a 301 redirect.
IIS7.5 Http Redirect it is not design for this situation so I suppose to use IIS 7.5 Url Rewrite Module.
My questions: how write the ROLE in my web.config? Thanks
Try adding something like this between the <System.webServer> tags in your web.config:
<rewrite>
<rules>
<rule name="Redirect mysite.hosting.com to mysite.co.uk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mysite.hosting.com" />
</conditions>
<action type="Redirect" url="http://mysite.co.uk/{R:0}" />
</rule>
</rules>
</rewrite>
Alternatively, you can do this using global rules by adding:
<rewrite>
<globalRules>
<rule name="Redirects to mysite.co.uk" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="mysite.hosting.com$" />
</conditions>
<action type="Redirect" url="http://mysite.co.uk/{R:0}" />
</rule>
</globalRules>
</rewrite>

Resources