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

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>

Related

web.config - 2nd URL rewrite rule not woking

My wordpress has 2 rules of URL rewrite, this is the 1st rule:
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
</conditions>
<action type="Rewrite" url="index.php"/>
</rule>
This is the second 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>
They both works independently. But if I combine both rules like below:
<rewrite>
<rules>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
</conditions>
<action type="Rewrite" url="index.php"/>
</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>
</rules>
</rewrite>
Only the first rule is working, the 2nd rule is ignored as if it does not exists. How to make the 2nd rule works together with 1st rule?
You can try this rule:
<rule name="Redirect to https" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="wordpress" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url=" index.php" />
</rule>
Best regards,
Sam

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

Errors with converting web.config to htaccess

I wanted to convert this configuration file into a htacess file.
I'm looking for a way to convert my rewrite rules to an .htaccess file. I couldn't find any tool to automatically do this and all I can get myself is 500 Internal Server Errors.
The web.config file looks like this:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)$" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?module={R:1}" />
</rule>
<rule name="RedirectUserFriendlyURL2" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&page=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^([^/]+)/page/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?module={R:1}&page={R:2}" />
</rule>
<rule name="RedirectUserFriendlyURL3" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&p=([^=&]+)&id=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL3" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?module={R:1}&p={R:2}&id={R:3}" />
</rule>
<rule name="RedirectUserFriendlyURL4" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&p=([^=&]+)&id=([^=&]+)&page=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}/{C:4}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL4" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/([^/]+)/page/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?module={R:1}&p={R:2}&id={R:3}&page={R:4}" />
</rule>
<rule name="RedirectUserFriendlyURL5" stopProcessing="true">
<match url="^index\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^module=([^=&]+)&p=([^=&]+)&date=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
</rules>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?page=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/page/{R:3}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL3" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?p=([^=&]+)&(?:amp;)?id=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL4" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?p=([^=&]+)&(?:amp;)?id=([^=&]+)&(?:amp;)?page=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/page/{R:5}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL5" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)index\.php\?module=([^=&]+)&(?:amp;)?p=([^=&]+)&(?:amp;)?date=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/{R:4}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="true" />
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.24:00:00" />
</staticContent>
</system.webServer>
</configuration>
So my solution was like this.
RewriteRule ^index\.php$ {C:1}
RewriteRule ^([^/]+)/?$ index.php?module=$1
RewriteRule ^index\.php$ {C:1}/{C:2}
RewriteRule ^([^/]+)/page/([^/]+)/?$ index.php?module=$1&page=$2
RewriteRule ^index\.php$ {C:1}/{C:2}/{C:3}
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?module=$1&p=$2&id=$3
RewriteRule ^index\.php$ {C:1}/{C:2}/{C:3}/{C:4}
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/page/([^/]+)/?$ index.php?module=$1&p=$2&id=$3&page=$4
RewriteRule ^index\.php$ {C:1}/{C:2}/{C:3}
Bus I'm having some problems with this code. Some rules are really not working. Will you help me to solve this problem.
Thanks in advance!
first you should check mod_rewrite.c is active in your server or not , then turn it on and start re-write urls like this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?module=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?module=$1&p=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?module=$1&p=$2&id=$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/page/([^/]*)$ /index.php?module=$1&p=$2&id=$3&page=$4 [L]
</IfModule>

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