IIS 7 URL Rewrite - 403 Error - iis

I know this question has been asked before, but I haven't found an answer to what I'm running into. I'm trying to redirect a domain (not a sub-domain) to a sub-folder. I'm running IIS 7 and I created a URL rewrite rule like this:
<rule name="subfolder" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(www\.)?domain.com$" />
<add input="{PATH_INFO}" pattern="subfolder" negate="true" />
</conditions>
<action type="Rewrite" url="/subfolder/{R:0}" />
</rule>
With the rule off, I can navigate to a page like http://domain.com/subfolder/index.htm. With it on, I get a 403 error when I try to navigate to http://domain.com/index.htm.
403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.
How do I fix this?

Try this simple rule:
<rule name="Redirect domain to sub-folder" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="http://domain.com/subfolder" />
</rule>

Related

Have Subdirectory of site in IIS show the index.html file when browsing to site

I am a fairly novice windows IIS guy, so any help I would greatly appreciate.
I have a client that requested all root Urls on their department websites redirect to their index.html page. So if a user, for example, goes to https://mysite.domain.com/, it will redirect to https://mysite.doman.com/index.html.
I did this through IIS 10 using a URLrewrite rule on the root of each site by doing the following.
<rule name="Index Request" enabled="true" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="https://mysite.domain.com/index.html" />
</rule>
This seemed to work.
The client now wants to have any of the subfolders of the root site show the index.html to any subdirectory sites of the root. Example. Https://mysite.domain.com/subdir/ “This is what shows now” to https://mysite.domain.com/subdir/index.html. Is there a way to do this in IIS?
Thanks in advance for any advice
I have made sure the index.html is the default document. I have also looked at User Friendly URL-template https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/user-friendly-url-rule-template, but I am not sure if this is the right direction to go.
You can try this rule:
<rule name="test" enabled="true" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/index.html" />
</rule>
<rule name="test1" enabled="true" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/([^/]+)" />
</conditions>
<action type="Redirect" url="/{C:1}/index.html" />
</rule>
<rule name="SEF Rule" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}/index.html" matchType="IsFile"
ignoreCase="false" />
</conditions>
<action type="Redirect" url="{R:1}/index.html" />
</rule>

IIS 10 URL Rewrite http traffic to https redirect not working

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>

Regex does not work for {URL} in the URL rewrite in IIS

Using URL rewrite in the IIS on windows 10. I want to check specific url in the IIS.
Below is the condition I need to check.
www.mtl173 or http://www.mtl173 or http://www.mtl173/ or mtl173/
Above all should be valid and should redirect to the given path.
I have checked {URL} so far as following.
<rule name="RuleForSite1" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^(http\:\/\/)?(www\.)?mtl173(\/)?$" />
</conditions>
<action type="Redirect" url="www.mtl173/app1/" />
</rule>
Note: when I test all the URLs in the IIS, all test passed but when I run the same URL from the browser, it shows me the default IIS page and does not redirect me to the app1/.
However, the above does not work and does not redirect the URL to the given path.
Any remediations for the issue I am facing?
Expected result:
When I enter any of the following URL, it should redirect me to the /app1.
www.mtl173
http://www.mtl173
http://www.mtl173/
mtl173/
You can use below url rewrite rule.
<rule name="RuleForSite1" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.mtl173|mtl173$" />
<add input="{REQUEST_URI}" pattern="app1" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mtl173/app1/" />
</rule>
You can learn about server variable from below links:
IIS Server Variables

IIS: Redirect subdomain to specific url

In IIS 8, I want to redirect the url http://test.example.com to http://www.example.com/abc/123
I try this, but not work.
<rule name="test" stopProcessing="true">
<match url="^test.example.com$" />
<action type="Redirect" url="http://www.example.com/abc/123" />
</rule>
you could add the pattern like this
<rule name="RedirectDomain" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="(.*)test.example.com />
</conditions>
<action type="Redirect" url="http://www.example.com/abc/123" redirectType="Permanent" />
</rule>
In the IIS GUI on the given side you should be able to choose 'HTTP Redirect' from there you can type in a url to redirect the site to.
I don't know if this approach is the recommended (It is normally used to redirect HTTP request on a given site to use HTTPS), but it will solve your problem.

Setting up URL Rewrite rule for a specific domain

For local dev testing, I need to catch all requests to www.somedomain.com/XXX (where X is the path), and send them on to localhost/somevdir/XXX.
I've added this to my HOSTS file (c:\windows\system32\drivers\etc):
127.0.0.1 www.mydomain.com
Then, in IIS8 (Windows 8), I've added a binding to my "Default Web Site" for the host www.mydomain.com. This works, I can now browse www.mydomain.com/test.html and see a test html page. My virtual dir is inside the Default web site. I then add a URL Rewrite URL to the web site for the last bit:
<rewrite>
<rules>
<rule name="mydomain.com" stopProcessing="true">
<match url="^www.mydomain.com/(.*)" />
<action type="Rewrite" url="localhost/MyVDir/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
But - that doesn't work. I get a 404 so it looks the the match never happens. I've tried redirect and rewrite, and I've tried without the ^ in the regex and a few other regex tweaks. Can someone explain what I've done wrong?
I think the following should work:
<rule name="mydomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(mydomain\.com|www\.mydomain\.com)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>
The match on any URL makes sure the conditions are checked, and the HTTP_HOST server variable seems the most reliable way of checking the requested hostname. You could remove the REQUEST_FILENAME input condition, but it works as quite a nice sanity check to make sure static files are always served.
The following is better for catching both www. and non-www. versions of the domain so that you don't have to write the domain twice, which could possibly cause errors with being twice as likely to write a typo. (The parenthesis with the ? character means optional in regex terms.)
<rule name="mydomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain\.com$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>

Resources