Rewrite policies in Azure Web App - azure

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.

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

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>

URL with www causing 404 in 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>

IIS URL Rewrite equivalent for Question2Answer mod_rewrite

What IIS URL Rewrite rule set in web.config is equivalent to this mod_rewrite rule set provided in Question2Answer's default .htaccess?
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
The rule differs from a typical WordPress-style rewrite in that nested path elements need to be passed through. Following is an example of a test URL that Question2Answer uses to verify rewriting is working, when Question2Answer is deployed into a qa directory on the server (deployment to the web root fails similarly):
http://localhost:32568/qa/url/test/%24%26-_~%23%25%5C%40%5E%2A%28%29%3D%21%28%29%5D%5B%60%27%3B%3A%7C%22.%7B%7D%2C%3C%3E%3F%23+%CF%80%C2%A7%C2%BD%D0%96%D7%A9?dummy=&param=%24%26-_%7E%23%25%5C%40%5E%2A%28%29%3D%21%28%29%5D%5B%60%27%3B%3A%7C%22.%7B%7D%2C%3C%3E%3F%23+%CF%80%C2%A7%C2%BD%D0%96%D7%A9
This is what IIS Manager's "Import mod_rewrite rules" feature came up with:
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="." ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^(.*)//(.*)$" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^.*$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" url="index.php?qa-rewrite={R:0}&{QUERY_STRING}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I also added this so that the test URLs would fail in the same way as the actual navigation URLs:
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
There is no answer to a question covering this on question2answer QA's site. Hopefully someone experienced in IIS URL Rewrite here will know.
This configuration works:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="DeduplicateSlashes" stopProcessing="true">
<match url="." ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^(.*)//(.*)$" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" />
</rule>
<rule name="CleanRouting" stopProcessing="true">
<match url="^.*$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?qa-rewrite={R:0}&{QUERY_STRING}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<!-- Double escaping is needed for URLs with '+', e.g. for the account page for a username with a space. -->
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
</configuration>

Resources