URL with www causing 404 in IIS - iis

I have a site at domain.com. I've recently installed an SSL certificate so I can use HTTPS.
In IIS, I created bindings for domain.com and www.domain.com for http and https. Then I have a URL rewrite rule in my web.config that adjusts every url to point to https://domain.com
My issue is that when I go to http://domain.com or https://domain.com the site works, but http://wwww.domain.com and https://www.domain.com do not. Is this an issue with my rewrite rules or my binding?
Here is my web.config for reference:
<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>
<rule name="Imported Rule 1">
<match url="^(register)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://domain.com/register.php" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>

i have solve this problem like this code block.
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Temporary" />
</rule>
</rules>
</rewrite>

Related

IIS rewrite rule all to index.php except /edit directory

Hello i am trying to make it so when i put in url /edit then put me in a directory /edit. Everything else is only in the index.php
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(\.png|\.jpg|\.gif|\.jpeg|\.js|\.css|\.ico|\.mp4|captcha.php|servertime.php|robots.txt)$" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
</rules>
Currently this code is redirecting everything to an index.php
When I add:
<rule name="Imported Rule 1-1" stopProcessing="true">
<match url="^" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^edit/?(.*)$" />
<action type="Rewrite" url="edit/index.php" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="index.php" />
</rule>
The directory /edit opens but I lose the other redirect all to index.php
You need a new "rule" just make sure you add it before the other rules :)
<rule name="rule1" stopProcessing="true">
<match url="^edit" />
<action type="None" />
</rule>

Redirect to www in iis?

I know there been quite a few posts on this already but the answers do not work for me.
when I type in example.com in the url I want it to redirect to https://www.example.com
I have these rules but when I put in example.com I get 404.
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTPS}" ignoreCase="true" matchType="Pattern" negate="false" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
edit
I also tried this but still not getting expected re
According to your url rewrite rule I found you set a url rewrite rule at first. That means if your url match the (.*), it will rewrite the url to /index.html and will not go to any other url rewrite rule. So your "Redirect to www" and "Redirect to HTTPS" rules will not be fired.
To solve this issue, I suggest you could try to use below rules. It will fire the redirect rules firstly, then fired rewrite rule.
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTPS}" ignoreCase="true" matchType="Pattern" negate="false" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
I have these rules but when I put in example.com I get 404.
As far as I know, 404 means page not found, I suggest you could try to use fail request tracing to track the actually url IIS have redirect to.
Details, you could refer to below article:
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules

How to redirect a url to https without www in web.config?

I want to do 301 permanent redirection of my domain from example.com to https://example.com (without www)
I am using the following rule:
<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>
But it always redirect me to https://www.example.com

Rewrite policies in Azure Web App

I'm contacting you because I'm having issues migrating an Apache + PHP app to Azure.
I was using Apache rewrite module to read the Request URIs and transform them to correct URLs. I've translated those instructions into IIS rewrite rules, but I can't make them work in Azure.
The rules were:
RewriteEngine on
RewriteRule ^(zpanel\/)(.*)\.json$ $1.json.php [QSA,L]
RewriteRule ^(zpanel\/)(.*)\-process\-add$ $1_procesar.php?abm_accion=a [QSA,L]
RewriteRule ^(zpanel\/)(.*)\-process\-edit\-(.*)$ $1_procesar.php?abm_accion=m&id=$2 [QSA,L]
RewriteRule ^(zpanel\/)(.*)\-process\-delete\-(.*)$ $1_procesar.php?abm_accion=d&id=$2 [QSA,L]
RewriteRule ^(zpanel\/)(.*)\-add$ index.php?put=$1_am&abm_accion=a [QSA,L]
RewriteRule ^(zpanel\/)(.*)\-edit\-(.*)$ index.php?put=$1_am&abm_accion=m&id=$2 [QSA,L]
RewriteCond %{REQUEST_URI} !(css|js|favicon|img|php|json|icon|process|report|fonts|ckeditor)
RewriteRule ^(zpanel\/)(.*)$ index.php?put=$1 [QSA,L]
So I've translated them to:
<rewrite xdt:Transform="InsertIfMissing">
<rules xdt:Transform="InsertIfMissing">
<clear/>
<rule name="rule 1d" stopProcessing="true">
<match url="^(zpanel\/)(.*)\.json$" />
<action type="Rewrite" url="/{R:1}{R:2}.json.php" appendQueryString="true" />
</rule>
<rule name="rule 1l" stopProcessing="true">
<match url="^(zpanel\/)(.*)\-process\-add$" />
<action type="Rewrite" url="/{R:1}{R:2}_procesar.php?abm_accion=a" appendQueryString="true" />
</rule>
<rule name="rule 1x" stopProcessing="true">
<match url="^(zpanel\/)(.*)\-process\-edit\-(.*)$" />
<action type="Rewrite" url="/{R:1}{R:2}_procesar.php?abm_accion=m&id={R:3}" appendQueryString="true" />
</rule>
<rule name="rule 1U" stopProcessing="true">
<match url="^(zpanel\/)(.*)\-process\-delete\-(.*)$" />
<action type="Rewrite" url="/{R:1}{R:2}_procesar.php?abm_accion=d&id={R:3}" appendQueryString="true" />
</rule>
<rule name="rule 1S" stopProcessing="true">
<match url="^(zpanel\/)(.*)\-add$" />
<action type="Rewrite" url="/{R:1}index.php?put={R:2}_am&abm_accion=a" appendQueryString="true" />
</rule>
<rule name="rule 1V" stopProcessing="true">
<match url="^(zpanel\/)(.*)\-edit\-(.*)$" />
<action type="Rewrite" url="/{R:1}index.php?put={R:2}_am&abm_accion=m&id={R:3}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^(zpanel\/)(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{URL}" pattern="^/css|js|favicon|img|php|json|icon|process|report|fonts|ckeditor$" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}index.php?q={R:2}" appendQueryString="true" />
</rule>
</rules>
But after this, I keep having the same error message in Azure. "The resource cannot be found".
Are the translations OK? Do I need to change something else to make it work on the Azure version of IIS?
Thanks
According to your description and error message, I guess you may have some error on your translations result.
As far as I know, IIS has a url rewrite module, which could tranlate the rewrite role.
I tranlate the rewrite role as below(not as same as yours), I suggest you could use below codes and try again:
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(zpanel\/)(.*)\.json$" ignoreCase="false" />
<action type="Rewrite" url="{R:1}.json.php" appendQueryString="true" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^(zpanel\/)(.*)\-process\-add$" ignoreCase="false" />
<action type="Rewrite" url="{R:1}_procesar.php?abm_accion=a" appendQueryString="true" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="^(zpanel\/)(.*)\-process\-edit\-(.*)$" ignoreCase="false" />
<action type="Rewrite" url="{R:1}_procesar.php?abm_accion=m&id={R:2}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 4" stopProcessing="true">
<match url="^(zpanel\/)(.*)\-process\-delete\-(.*)$" ignoreCase="false" />
<action type="Rewrite" url="{R:1}_procesar.php?abm_accion=d&id={R:2}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 5" stopProcessing="true">
<match url="^(zpanel\/)(.*)\-add$" ignoreCase="false" />
<action type="Rewrite" url="index.php?put={R:1}_am&abm_accion=a" appendQueryString="true" />
</rule>
<rule name="Imported Rule 6" stopProcessing="true">
<match url="^(zpanel\/)(.*)\-edit\-(.*)$" ignoreCase="false" />
<action type="Rewrite" url="index.php?put={R:1}_am&abm_accion=m&id={R:2}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 7" stopProcessing="true">
<match url="^(zpanel\/)(.*)$" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="(css|js|favicon|img|php|json|icon|process|report|fonts|ckeditor)" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?put={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
You could click the url rewrite module in the IIS explorer firstly.
Then you could find import rule at right side. Click the button you will find the translate windows.

Redirect rule with exclude subdomain

Trying to set the redirect rule which would force redirect for everything with the exception of one sub domain and one web page:
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" negate="true" pattern="^www1\.mywebsite\.com$" ignoreCase="true" />
<add input="{REQUEST_URI}" negate="true" pattern="^/file\.txt$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
<outboundRules>
<rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
<match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
</conditions>
<action type="Rewrite" value="max-age=31536000; includeSubDomains; preload" />
</rule>
</outboundRules>

Resources