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>
Related
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>
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.
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=¶m=%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>
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 :)
I have next code for .htaccess which works very nice on Apache
RewriteEngine On
RewriteBase /folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule index.php.* - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?id=$1
But I need to run in on server with IIS. Could you help me to translate it for IIS? Thanks a lot.
If you're using IIS 7, you can install URL Rewrite extension for IIS provided by microsoft. This extension also has feature to import .htaccess files. Sometimes, not all of lines can be conveted (see second line of code below). We have to make a little adjustment. Copy and paste the results from 'XML View' tab in import dialog into text editor, and save it as 'web.config' file. Here is your .htaccess converted:
<rewrite>
<!--This directive was not converted because it is not supported by IIS: RewriteBase /folder.-->
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="index.php.*" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2">
<match url="^(.*)" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?id={R:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
NOTE: direct import result, not yet adjusted.