IIS URL Rewrite equivalent for Question2Answer mod_rewrite - iis

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>

Related

Convert Apache .htaccess rules to IIS web.config to append .php file extensions

I am using below .htaccess code. But I need to convert my .htaccess file content to web.config file. I have no idea how to do it. please check my .htaccess code below:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
How can I convert this to web.config?
we can put below content to the web.config file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/{R:1}.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
<system.web>
<compilation tempDirectory="D:\WWW\ovintours.com\tmp" />
</system.web>
</configuration>

.htaccess in hosting Windows

I recently bought a web hosting with windows (IIS).
The site that I have to transfer to hosting uses the following .htaccess file
Options -Indexes
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)(\.)(.*)$ index.php?url=$1.$3 [L,QSA]
RewriteRule ^ajax$ _res/ajax.php [QSA]
#RewriteRule ^(.*)$ index.php?t=$1 [L,QSA]
</IfModule>
On the new server that uses IIS it does not work, how do I solve it?
In your website web.config file add the rewrite section under the system.webServer section as shown below:
<system.webServer>
<rewrite>
<rules>
<rule name="Rule 1" stopProcessing="true">
<match url="^(.*)(\.)(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{URL}" pattern="^/favicon.ico$" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}.{R:3}" appendQueryString="true" />
</rule>
<rule name="Rule 2">
<match url="^ajax$" ignoreCase="false" />
<action type="Rewrite" url="_res/ajax.php" appendQueryString="true" />
</rule>
<rule name="Rule 3" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="index.php?t={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
Also, ensure that you have Url Rewrite IIS extension installed on your server.

Url rewrite Azure PHP?

I have created some PHP app on Azure but this is my first time i am using IIS for my PHP application. I know how to use .htaccess for url rewrite on Apache and there is no problem. But how to accomplish that on Azure IIS. Here is my .htaccess for Apache.
.htaccess
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
You need to use your Web.config file and use the <system.webServer> and <rewrite> rules within this.
You will need to use the following inside your web.config to replicate the code you have above from your .htaccess:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" 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.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Proper web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
<defaultDocument>
<files>
<clear />
<add value="index.php" />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="Default.aspx" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="ModX IIS7 Rule 1 (By Simon Fraser)" stopProcessing="true">
<match url=".*" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_USER_AGENT}" pattern="^.*internal\ dummy\ connection.*$" />
</conditions>
<action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
</rule>
<rule name="ModX IIS7 Rule 2 (By Simon Fraser)" stopProcessing="true">
<match url="^(manager|assets)" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="ModX IIS7 Rule 3 (By Simon Fraser)" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" pattern="" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="index.php?q={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

web.config trouble with rewriting the numeric id as a query_string

I want a rewrite rule in my web.config that will translate the following urls:
mypage to mypage.php
mydirectory/mypage to mydirectory/mypage.php
mydir2/mydir1/mypage to mydir2/mydir1/mypage.php
mypage/12345 to mypage.php?id=12345
mydirectory/mypage/12345 to mydirectory/mypage.php?id=12345
mydir2/mydir1/mypage/12345 to mydir2/mydir1/mypage.php?id=12345
As a .htaccess file in Apache, I simply do this with
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\/([0-9]+)$ $1.php?id=$2&%1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)$ $1.php?%1 [L]
I tried reproducing this behaviour in IIS with a web.config. Here's what I have so far:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="hide .php extension without losing ?id=" stopProcessing="true">
<match url="^(.*)/([0-9]+)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.php" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.php?id={R:2}" />
</rule>
<rule name="hide .php extension" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.php" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But when I go to my browser and type http://localhost/mydirectory/mypage/12345, I get a 404 page not found. I see on the 404 page that request url is http://localhost:80/mydirectory/mypage/12345 and physical path is C:\inetpub\wwwroot\mydirectory\mypage\12345.
What am I doing wrong?
Oh wait, I figured it out. I replaced the node name="hide .php extension without losing ?id=" with
<rule name="hide .php extension without losing ?id=" stopProcessing="true">
<match url="^(.*)/([0-9]+)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.php?id={R:2}" />
</rule>

Converting htaccess to web.config causing 500 error and I can't see, why?

There I'm really stuck here, and its driving me insane. Ive searched all over but I can't see what I'm doing wrong here.
I'm moving an application that was previously on a windows server with ISAPI_ModRewrite so this worked with htaccess, I'm trying to move this to a windows-server 2008 server which I cant have ISAPI installed on as its a shared hosting environment.
Below is what I'm trying to do but this is just returning a 500 internal server error any ideas what I'm doing wrong? this is the htaccess code I'm trying to convert...
# PRODUCT PAGE
RewriteRule ^([0-9]*)/(.*)/? /prodpage.asp?productid=$1 [L]
# OTHER PAGES
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ /$1.asp [QSA]
RewriteRule ^([^/]+)/([0-9]+)-([0-9]+)/$ /$1.asp?pricerange=$2-$3
RewriteRule ^([^/]+)/([0-9]+)-([0-9]+)/([a-z])/$ /$1.asp?pricerange=$2-$3&sort=$4
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
And this is my web.config file...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Product Page" stopProcessing="true">
<match url="^([0-9]*)/(.*)/?$" 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="prodpage.asp?productid={R:1}" appendQueryString="true" />
</rule>
<rule name="Other Page1" 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="{R:1}.asp" appendQueryString="true" />
</rule>
<rule name="Other Page2" stopProcessing="true">
<match url="^([^/]+)/([0-9]+)-([0-9]+)/$" 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="{R:1}.asp?pricerange={R:2}-{R:3}" appendQueryString="true" />
</rule>
<rule name="Other Page3" stopProcessing="true">
<match url="^([^/]+)/([0-9]+)-([0-9]+)/([a-z])/$" 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="{R:1}.asp?pricerange={R:2}-{R:3}&sort={R:4}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
fixed this sorry, thought i would post my answer for those that may need help in future.
i needed to replace the & symbol with & as its xml. Thanks anyway :)

Resources