I have installed URL rewrite module on the IIS 8.0 and configure rules
If user come without www and it will prefix www
If user comes from http then it will redirect to https
If user comes from mobile browser than it sends to mobile website
Below are the rules
<appcmd>
<CONFIG CONFIG.SECTION="system.webServer/rewrite/globalRules" path="MACHINE/WEBROOT/APPHOST" overrideMode="Inherit" locked="false">
<system.webServer-rewrite-globalRules>
<rule name="Mobile Redirect" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^$" ignoreCase="true" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="android|blackberry|googlebot-mobile|iemobile|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>
<serverVariables>
</serverVariables>
<action type="Redirect" url="/en-mobile" appendQueryString="false" />
</rule>
<rule name="Add HTTPS and WWW prefix to website.COM" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^website\.com" />
</conditions>
<serverVariables>
</serverVariables>
<action type="Redirect" url="https://www.website.com/{R:1}" appendQueryString="false" />
</rule>
<rule name="Add HTTPS to WWW.website.COM" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<serverVariables>
</serverVariables>
<action type="Redirect" url="https://www.website.com/{R:1}" />
</rule>
</system.webServer-rewrite-globalRules>
</CONFIG>
</appcmd>
In above first rule Mobile redirect is done when user come from mobile browser. I redirect to https://www.website.com/en-mobile but when I do like this https://m.website.com/en-mobile it gives error but when I browse manually it works very good. So how can I redirect to that url when people come from https://www.website.com to https://m.website.com/en-mobile
I could solve this issue may be it will help to somebody so I am adding my solution over here. I change Mobile Redirect rule action to
<action type="Redirect" url="https://m.website.com/en-mobile" appendQueryString="true" redirectType="Found" />
Putting redirectType to found solves my issue.
Related
I have two domains. Both read from the same source. That is, the path of their files on the server is the same with same web.config file. I want both of them to be redirect to the HTTPS path without WWW, regardless of the conditions that are called in the URL. Similar to below
HTTP to HTTPS
WWW to non-WWW
for example.com and example.co
I tried to use the below code:
<rewrite>
<rules>
<rule name="Redirect to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Special case for HTTPS with www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^ON$" />
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But this code cannot be considered for both domains com and co. Because it redirects only to the com domain.
Try splitting the two different domain names into two rules, and add the full hostname to your rules, which will trigger your rule for a specific domain only:
<rewrite>
<rules>
<rule name="Redirect example.com to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?example.com$" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect example.co to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?example.co$" />
</conditions>
<action type="Redirect" url="https://example.co/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
I've seen a lot of answers that come close, like this one: IIS Redirect non-www to www AND http to https but it's does not exactly match my case.
I have a website that has 2 domain aliases on top of its main domain name. Displaying the right content for each version is done in the code itself.
I am trying to use the WEB.CONFIG file to rewrite in 2 cases: when HTTPS is not used and/or when WWW is absent at the beginning of the url.
So http://example.com, https://example.com and http://www.example.com would all be rewritten as https://www.example.com Same thing if the site is visited using one of its domain aliases, for example http://examplealias.com would become https://www.examplealias.com
This answer
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
will not work for me as I cannot hard code the domain name.
I have tried this
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
but this results in https://www.www.example.com if I call http://www.example.com
What would be the right way to combine both conditions?
Okay, apparently in my case, it's better NOT to combine the rules:
<rule name="Redirect naked domains (that do NOT have a custom sub-domain) to www.domain.com" enabled="true" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www." negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Redirect non-https urls to https" enabled="true" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
This works perfectly.
I have the following problem:
i want to let the users of an exact url redirect to another one.
Note: my URL immediately downloads a file.
What i want is to let the file forward to another file (it needs to download the file i'm redirecting to).
My Rule:
<rule name="File" patternSyntax="ExactMatch" stopProcessing="true">
<match url="https://SITE.be/download/DownloadFile?id=138999" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="https://SITE.be/download/DownloadFile?id=138999" />
<add input="{QUERY_STRING}" pattern="https://SITE.be/download/DownloadFile?id=138999" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
</conditions>
<action type="Redirect" url="https://SITE.be/download/DownloadFile?id=138111" appendQueryString="false" />
</rule>
my rule isn't working, it doesn't redirect it and when i check 'URL redirect checkers' it mentions that there isn't a redirect set on the URL.
what am i doing wrong?
Edit:
I tried:
<rule name="File" stopProcessing="true">
<match url="download/DownloadFile?id=138999" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^site.be$" />
</conditions>
<action type="Redirect" url="https://site.be/download/DownloadFile?id=138111" appendQueryString="false" />
</rule>
with no improving (redirect checkers on google still don't trigger that theres a redirect set)
According to your iis rewirte code, I find it has problem in match url part.
This part could only match the [download/DownloadFile] not the querystring.
If we want to check the query string id, we should use url rewrite condition.
More details, you could refer to below url rewrite rule.
<rule name="File" stopProcessing="true">
<match url="download/DownloadFile" />
<conditions logicalGrouping="MatchAll">
<add input="{QUERY_STRING}" pattern="id=138999" />
<add input="{HTTP_HOST}" pattern="^site.be$" />
</conditions>
<action type="Redirect" url="https://site.be/download/DownloadFile?id=138111" appendQueryString="false" />
</rule>
Or
<rule name="File" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="sitebe" />
<add input="{REQUEST_URI}" pattern="download/DownloadFile\?id=138999" />
</conditions>
<action type="Redirect" url="https://site.be/download/DownloadFile?id=138111" appendQueryString="false" />
</rule>
I'm trying to write a URL rewrite rule to force a HTTPS connection. This should always happen except when a request is using localhost (e.g. http://localhost/mysite).
The rule is configured as following:
<rule name="Redirect to https" enabled="true" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{URL}" pattern="localhost" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
I also tried to use ^localhost and ^localhost/(.*) as a pattern for the URL condition with no help.
Does anyone have an idea why this does not work and what a solution for this problem should be?
Your code should look like this instead
<rule name="Redirect to https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
I'm trying to turn off HTTPS when someone hits the root of my site...
I want
https://www.domain.com/
to redirect to
http://www.domain.com/
but I also want..
https://www.domain.com/secure.aspx
or any other named page not to redirect.
Here's what I have so far but I can't get it to work. I tried a thousand ways, and read all the IIS documentation and examples on this, but I can't come up with the magic formula.
<rule name="Redirect Root Only to Non HTTP" stopProcessing="true">
<match url="\.com/$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="http://www.domain.com" appendQueryString="false" redirectType="Found" />
</rule>
I think this is your answer:
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^.*\.aspx$" ignoreCase="true" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTP" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^$" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/" redirectType="Permanent" />
</rule>
You were close .. except the actual pattern (which matches URL path part only and not domain name). The proper pattern is ^$ which means empty string which will ONLY match domain root hit e.g. https://www.domain.com/.
The full rule will be:
<rule name="Redirect Root to HTTP" stopProcessing="true">
<match url="^$"/>
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="on"/>
</conditions>
<action type="Redirect" url="http://www.domain.com/" redirectType="Permanent"/>
</rule>