.htaccess in hosting Windows - .htaccess

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.

Related

How to fix this issue: The rule set cannot be converted into an equivalent IIS format because control flow flags (C, S, N) are not supported

I am moving a website from a Linux server to Windows 2016 and need to import the .htaccess rewrite rules. I get the above error on these rules:
# if this request is for "/" or has already been rewritten
RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
RewriteCond $1 \.(css|js)$ [NC,OR]
# or if root images folder
RewriteCond %{REQUEST_URI} ^/images(.*) [NC,OR]
# or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
RewriteRule ^(.*)$ - [S=1,L]
RewriteCond $2 \.(gif|jpg|png|jpeg)$ [NC]
RewriteRule (.+)/images/([^/]+) /images/$2 [S=1,L]
# else rewrite the request
RewriteRule . /index.php [QSA,L]
I have very little experience with Linux and hope someone can help out with a solution to this.
According to your description, I suggest you could try to use below url rewrite rules:
<rewrite>
<rules>
<!--# then skip the rewrite to WP-->
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<!--# if this request is for "/" or has already been rewritten-->
<add input="{R:1}" pattern="^(index\.php)?$" ignoreCase="false" />
<!--# or if request is for image, css, or js file-->
<add input="{R:1}" pattern="\.(css|js)$" />
<!--# or if root images folder-->
<add input="{URL}" pattern="^/images(.*)" />
<!--# or if URL resolves to existing file-->
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
<!--# or if URL resolves to existing directory-->
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="(.+)/images/([^/]+)" ignoreCase="false" />
<conditions>
<add input="{R:2}" pattern="\.(gif|jpg|png|jpeg)$" />
</conditions>
<action type="Rewrite" url="/images/{R:2}" />
</rule>
<!--# else rewrite the request-->
<rule name="Imported Rule 3" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Rewrite" url="/index.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>

Redirect from non-www to www using 301 on IIS

I have hosted my website on a windows server on 1&1.
There, is not possible add on the web.config the the elements <rewrite><rules>... So I am using an .htaccess file...
I am not so expert, I expected it would work just on Apache server. I was wrong! My .hataccess works also on IIS.
Here the code:
//Rewrite to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
//Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
The problem the redirect is done using a 302. I want a 301 redirect. I am using fiddler and this is the result:
The website where I have the problem is www.renovahaus.it
How can i solve my problem?
Thanx
This is normally done with the URLRewrite module, in the case of 1&1 this is disabled in the web.config however when you use a .htaccess file the contents of the .htaccess are still translated to the underlying web.config.
You can read a great tutorial on the conversion process here
But the summary is you are probably looking for the code block :-
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Which should transparently add the following to the web.config file (Even though you can't edit it directly)
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.example.com/{R:1}" />
</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" />
<add input="{URL}" pattern="^/favicon.ico$" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?q={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>

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>

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>

Translate .htaccess to web.config for IIS

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.

Resources