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