How do I enforce www on an IIS hosted website? - iis

My site is hosted on IIS, I need to enforce the browser to use www prefix.
I've installed the Url Rewrite Module and my rule is:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Add WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
from IIS7 URL Rewrite - Add "www" prefix
But I cannot work out how to maintain ssl

You need to capture the protocol in the input:
<rule name="Enforce WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
{C:1} will contain the protocol and {C:2} will have your domain and anything else.
(source)

Related

IIS multiple rewrite rules for Virtual directory

i have a website and one virtual directory called prog .looking an iis redirect rule for the following condition.
1.i need to redirect all non www http request http://domain/prog to https://www.domain/prog
2.redirect http www request http://www.domain/prog to https://www.domain/prog
3.redirect https://domain/prog to https://www.domain/prog
i have follow the below url
iis url redirect http to non www https
but one condtion not working
https://domain/prog to https://www.domain/prog
<rewrite>
<rules>
<rule name="SecureRedirect" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://www.{C:2}/prog" redirectType="Permanent" />
</rule>
</rules>
i have applied url rewrite on prog Virtual directory and its worked
<rewrite>
<rules>
<clear />
<rule name="Redirect non-www OR non-https to https://www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^domain.com$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.domain.com/prog/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>

Redirect and remove www and add https

This rule seems to work fine, but when I go to https://www.example.com it doesn't remove the www.
However, if I go to http://www.example.com it works as expected.
Why is it not redirecting from https?
<rewrite>
<rules>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$"/>
</conditions>
<action type="Redirect" url="https://example.com{PATH_INFO}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
Per my comment above, your rule actually worked for me in the scenario that you described. However, I do not think that it handles the case when a request is made to an insecure url that does not contain www in the hostname. For example, http://example.com/blah/ would not be redirected. The following rule below handles that case as well:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Remove WWW and Ensure HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://example.com{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The rule checks for a hostname starting with www or an insecure request. If either condition is met, the user is redirected.

IIS rewrite change filename

I have some old URLs in a forum application points to http://mydomain.tld/showthread.php?p=xxxx where xxxx is the an integer id.
My new forum application uses viewtopic.php?p=xxxx and I want to handle this using IIS web.config rewrite rules.
I have tried to add the last rule as the following:
<rewrite>
<rules>
<rule name="Redirect www to non www" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://domain.tld/{R:1}" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.tld" negate="true" />
</conditions>
</rule>
<!-- this for change from showthread.php to viewtopic.php -->
<rule name="Rewrite" stopProcessing="true">
<match url="(.*)showthread\.php" />
<action type="Rewrite" url="viewtopic\.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
However, it does not able to redirect to the viewtopic.php. Any suggestion?
I have to enable httpRedirect in the IIS. Then I used this redirect rule in the web.config at the root of my forum:
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="*showthread.php" destination="/viewtopic.php?$P" />
</httpRedirect>

Force HTTPS via URL rewrite in web.config

I'm trying to force HTTPS via a web.config URL rewrite rule for a URL which includes a path.
eg http://my.domain.com/folder must be forced to use https://my.domain.com/folder
I'd also like to force HTTPS for all pages on their relative paths
eg http:// my.domain.com/folder/page.aspx forced to https:// my.domain.com/folder/page.aspx
This is what I have:
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
<add input=”{REQUEST_METHOD}” pattern=”^get$|^head$” />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
Try this URL rewrite rule:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<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>
</rules>
</rewrite>
</system.webServer>
</configuration>
Check your casing and the type of quotes you are using. Also, the 2nd input method is invalid - the pattern isn't valid a regex. This rule works for what you want:
<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="Permanent" />
</rule>

IIS URL rewrite in child virtual directory not redirecting

I have Asp.NET Application installed on 'default web site\orchard'... accessible at http://localhost/orchard, and I want to use URL Rewrite. I added rules:
<rewrite>
<rewriteMaps>
<rewriteMap name="Blogger">
<add key="/aaa" value="/tags/tag1" />
</rewriteMap>
</rewriteMaps>
<rules>
<clear />
<rule name="Rewrite rule1 for Blogger" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Blogger:{REQUEST_URI}}" pattern="(.*)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But when I go to http://localhost/orchard/aaa then error 404 is returned instead of redirecting to http://localhost/orchard/tags/tag1.
When I put my web application in the root folder of website redirection works. http://localhost/aaa is redirected to http://localhost/tags/tag1.
What I'm doing wrong?
Thanks for help.

Resources