IIS - HTTPS and WWW Redirect - url

So i'm using iis 8 and trying to redirect all versions of my site to 1.
Say for example:
http://www.osrshub.com
www.osrshub.com
osrshub.com
http://osrshub.com
https://osrshub.com
https://www.osrshub.com
When a user hits any of these links i need it to redirect to 1 main link.
https://www.osrshub.com
Im using windows server 2012

This rule will redirect everything to https://www.osrshub.com:
<rule name="All in one rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^www\.osrshub\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.osrshub.com/{R:0}" />
</rule>
You need to add this rule in your web.config file

Related

301 redirect is not working properly

I've had a problem with 301 redirect. We've got a site (https://domain) and wanted to set preferred domain (non-www), for this - i verify 2 versions (https domain and https www.domain) with google analytics and html page.
And when i choose a preferred domain - it gives me This Error: Part of the process of setting a preferred domain is to verify that you own http www.domain/. Please verify http www.domain/.
Ok. I delete my https www.domain/ - version, and verify http www.domain/ - version (without "s")... And it works! BUT!
My seo tools(seositecheckup, woorank...) - said, that i didnt have 301 redirect yet..
How it is possible?
When I'm verifying http version - it works, but tools are not seeing theese changes.
When I'm verifying all 4 versions (2(http and https) with www and 2 without www) - it works, but tools are not seeing theese changes too.
When I'm verifying https version - search consule didnt allow me to set my preferred domain, by giving me This Error: Part of the process of setting a preferred domain is to verify that you own http www.domain. Please verify http www.domain
Here is my code on web config:
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://domain/{R:0}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Please, help!
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain.com" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.domain.com/{R:1}" />
</rule>
Try this code

Remove folder name in URL in Winhost

I have 2 domain names: domain1.com and domain2.com.
I am using the redirect script in the root to redirect each domain to it's own folder. This is working correctly. The problem I am having is that when it redirects to a folder it shows the folder name in the url after the domain name: (domain1.com/subfolder1).
My question is how to remove the ""subfolder1" from the URL?
I've tried to find a solution to this and found some guides out there but none seem to work. I have hosted nopcommerce in "subfolder1"
My web.config file as follows
<rewrite>
<rules>
<rule name="rule1" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain1.com$" />
<add input="{HTTP_HOST}" pattern="^www.domain1.com$" />
<add input="{PATH_INFO}" pattern="^/subfolder1/" negate="true" />
</conditions>
<action type="Redirect" url="\subfolder1\{R:1}" />
</rule>
</rules>
</rewrite>

How to exclude specified folder in IIS rewrite redirection rules

I have a rewrite rule that redirect to mobile sites. Redirection is working properly but I want to exclude one folder say ~/Content . I am stuck in rewriting the rules to exclude one folder. Below is the rule which does the redirection to mobile site
<rule name="Mobile Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="android|blackberry|googlebot-mobile|iphone|ipod|opera mobile|palmos|webos" />
<add input="{HTTP_X-Device-User-Agent}" pattern="midp|mobile|phone" />
<add input="{HTTP_X-OperaMini-Phone-UA}" pattern="midp|mobile|phone" />
</conditions>
<action type="Redirect" url="http://localhost/" appendQueryString="false" redirectType="Found" />
</rule>

How do can I code a rewrite rule in IIS 8 so that all traffic points to https://www.*.com from http://*.com?

I have the following root level URLs where people could possibly go:
1. http://example.com
2. http://www.example.com
3. https://example.com
4. https://www.example.com
How can I ensure that 1, 2, and 3 always redirect to 4 with a rewrite rule in IIS 8? Of course, it should keep whatever secondary information is attached (e.g. /images/myfile.jpg?query=string&another=string)
You can try this rule
<rewrite>
<rules>
<rule name="example.com to www.example.com" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="www.example.com" negate="true" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}"
redirectType="Permanent" />
</rule>
</rules>
<rewrite>

URL Rewrite on IIS from http to https is not working,

I have a problem. On IIS I got a web-site with two ports 80 and 443(https). I want to redirect all the http requests from user to https. I also added Rewrite rule to https, but when I enter in browser http://localhost/site it gives me the same page. I need to redirect user to httpS://localhost/site.
Maybe this is because of my local configurations?
And I disable Require SSL on IIS.
The rule is:
<rewrite>
<rules>
<rule name="HTTPS Redirect">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
</rules>
</rewrite>
Thank you.
Below is the exact rule we use on a production IIS 7 site to redirect all request from HTTP to HTTPS
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
There are some minor differences between what you have posted and what we use. Also, since you are running on local host, you wouldn't be using the built-in web server with visual studio would you? I don't think it will process IIS rewrite rules.
I realize this may not be your issue, however I had a similar debacle that was cause by a different problem.
I had enabled Require SSL and that caused the site to continually return a 403. So to use this method it appears you must disable SSL Settings -> Require SSL.
Hope this helps someone.
Also, if you have more than one rule, order could matter. Be sure to add the redirect rule before other rules or redirect may not fire.
Here is an example that is using SPA that required the rule order.
<rules>
// Adding this as last rule will cause it to not redirect
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="static dist files" stopProcessing="true">
<match url="^(.+)" />
<conditions>
<add input="{APPL_PHYSICAL_PATH}app\{R:1}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/app/{R:1}" />
</rule>
<rule name="index.html as document root" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="/app/" />
</rule>
<rule name="SPA Routes" stopProcessing="true">
<match url=".*|.*/.*$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/app/" />
</rule>
</rules>
It works for me in IIS 8.5
redirect
Three points:
Use single word OFF rather than ^OFF$
Use url="https://{HTTP_HOST}/{REQUEST_URI}"
Use redirectType=Permanent rather than Found although both are working but preferable Permanent type in this case
The webconfig code
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
you can add this to your pages. to force redirection.
if (!Request.IsSecureConnection)
{
Uri uri = new Uri(Request.Url, Request.RawUrl);
Response.Redirect(string.Format("https://{0}{1}{2}", uri.Host.StartsWith("www.x.com") ? uri.Host : "www.x.com", uri.AbsolutePath, uri.Query));
}
I just seen your tag of mvc
add this attribute to your actions. or controllers.
[RequireHttps]

Resources