IIS Rule Convert To .htaccess Rule - .htaccess

I need some help writing a .htaccess rule that:
Removed the .php extension from a file name
Rewrites variables.
So, a url like www.example.com/contact-us/456/789 would be interpereted by the server as www.example.com/contact-us.php?a=123&b=456&c=789
The following IIS web.config rule works a treat (using a rewrite map):
<rule name="Dynamic Rewriting" stopProcessing="true">
<match url="^([^/]+)(?:/?([^/]+)?)(?:/?([^/]+)?)(?:/?([^/]+)?)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{DynamicRewriting:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="/{C:1}.php?page={C:1}&a={R:2}&b={R:3}&c={R:4}" />
</rule>
<rewriteMap name="DynamicRewriting">
<add key="contact-us" value="contact-us" />
</rewriteMap>
Can anyone show me the .htaccess rule I need?

Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^/?([^/]+)(?:/?([^/]+)?)(?:/?([^/]+)?)(?:/?([^/]+)?)/?$ /$1.php?page=$1&a=$2&b=$3&c=$4 [L,QSA]

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>

.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.

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>

Mod_rewrite in Azure

I was write simple .htaccess and test it on Windows Azure Web Sites, but mod_rewrite didn't work there. Why? How I can reconfigurate my Azure?
RewriteEngine on
AddDefaultCharset utf-8
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test.html$ test.php?%{QUERY_STRING} [L]
.htaccess file is not recognizible by Azure Web Sites.
Azure Web Sites run on Microsoft IIS.
IIS has a URL Rewrite module, very similar to mod_rewrite for Apache. You can configure URL Rewrite rules by having a web.config file in your site root folder.
Follow the Creating Rewrite Rules article and scroll down to the "View the rule in Config file" to get an idea what it looks like.
Your rules defined, will look like this in web.config (and will most probably work as expected):
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^test.html$" 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" />
</conditions>
<action type="Rewrite" url="test.php?{QUERY_STRING}" appendQueryString="false" />
</rule>
</rules>
</rewrite>

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