Translate .htaccess Content to IIS web.config

Many PHP applications are distributed with configuration files for the Apache Web server. These configuration files (usually called .htaccess files) contain a number of settings that can be used for integrating the application with the capabilities of the Web server.
IIS 7 uses a file called Web.config to hold settings for integration with applications. The Web.config file contains information that control module loading, security configuration, session state configuration, and application language and compilation settings. Web.config files can also contain application-specific items such as database connection strings.
This article describes the most common uses of the .htaccess file by PHP applications, and shows how to use the Web.config file for these same functions in IIS.

Sample Application Configuration Files

The following examples are two configuration files for a sample application: an .htaccess file and a Web.config file.
Sample Application .htaccess File
#
# Apache/PHP/Application settings:
#

# Protect files and directories from prying eyes.

Order allow,deny


# Don't show directory listings for URLs which map to a directory.
Options -Indexes

# Follow symbolic links in this directory.
Options +FollowSymLinks

# Make Application handle any 404 errors.
ErrorDocument 404 /index.php

# Force simple error message for requests for non-existent favicon.ico.

ErrorDocument 404 "The requested file favicon.ico was not found.


# Set the default handler.
DirectoryIndex index.php

# Override PHP settings. More in sites/default/settings.php
# but the following cannot be changed at runtime.

# PHP 4, Apache 1.

php_value magic_quotes_gpc 0
php_value register_globals 0
php_value session.auto_start 0
php_value mbstring.http_input pass
php_value mbstring.http_output pass
php_value mbstring.encoding_translation 0


# PHP 4, Apache 2.

php_value magic_quotes_gpc 0
php_value register_globals 0
php_value session.auto_start 0
php_value mbstring.http_input pass
php_value mbstring.http_output pass
php_value mbstring.encoding_translation 0


# PHP 5, Apache 1 and 2.

php_value magic_quotes_gpc 0
php_value register_globals 0
php_value session.auto_start 0
php_value mbstring.http_input pass
php_value mbstring.http_output pass
php_value mbstring.encoding_translation 0


# Requires mod_expires to be enabled.

# Enable expirations.
ExpiresActive On

# Cache all files for 2 weeks after access (A).
ExpiresDefault A1209600

# Do not cache dynamically generated pages.
ExpiresByType text/html A1


# Various rewrite rules.

RewriteEngine on

# If your site can be accessed both with and without the 'www.' prefix, you
# can use one of the following settings to redirect users to your preferred
# URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
#
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# adapt and uncomment the following:
# RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
#
# To redirect all users to access the site WITHOUT the 'www.' prefix,
# (http://www.example.com/... will be redirected to http://example.com/...)
# uncomment and adapt the following:
# RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
# RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

# Modify the RewriteBase if you are using Application in a subdirectory or in a
# VirtualDocumentRoot and the rewrite rules are not working properly.
# For example if your site is at http://example.com/application uncomment and
# modify the following line:
# RewriteBase /application
#
# If your site is running in a VirtualDocumentRoot at http://example.com/,
# uncomment the following line:
# RewriteBase /

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


# $Id: .htaccess,v 1.90.2.1 2008/07/08 09:33:14 goba Exp $


Sample Application Web.config File
<?xml version="1.0" encoding="UTF-8"?>

<configuration>
<configsections>

<sectiongroup name="system.webServer">

<sectiongroup name="rewrite">

<section name="rewriteMaps" overrideModeDefault="Allow" />

<section name="rules" overrideModeDefault="Allow" />

</sectionGroup>

</sectionGroup>

</configSections>
<system.webServer>

<security>

<!-- This section should be uncommented after

installation to secure the installation. -->

<!--

<requestfiltering>

<denyurlsequences>

<add sequence="engine" />

<add sequence="inc" />

<add sequence="info" />

<add sequence="module" />

<add sequence="profile" />

<add sequence="po" />

<add sequence="sh" />

<add sequence="theme" />

<add sequence="tpl(\.php" />

<add sequence="Root" />

<add sequence="Tag" />

<add sequence="Template" />

<add sequence="Repository" />

<add sequence="code-style" />

</denyUrlSequences>

<fileextensions>

<add fileExtension=".sql" allowed="false" />

<add fileExtension=".pl" allowed="false" />

</fileExtensions>

</requestFiltering>

-->

</security>

<directorybrowse enabled="true" />

<caching>

<profiles>

<add extension=".php" policy="DisableCache" kernelCachePolicy="DisableCache" />

<add extension=".html" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="14:00:00:00" />

</profiles>

</caching>

<rewrite>

<rules>

<rule name="block favicon" stopProcessing="true">

<match url="favicon\.ico" />

<action type="CustomResponse" statusCode="404" subStatusCode="1"

statusReason="The requested file favicon.ico was not found"

statusDescription="The requested file favicon.ico was not found" />

</rule>

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

<defaultdocument>

<files>

<remove value="index.php" />

<add value="index.php" />

</files>

</defaultDocument>
<!-- HTTP Errors section should only be enabled if the "Error Pages"

feature has been delegated as "Read/Write" at the Web Server level.

<httperrors>

<remove statusCode="404" subStatusCode="-1" />

<error statusCode="404" prefixLanguageFilePath="" path="/index.php" responseMode="ExecuteURL" />

</httpErrors>

-->
</system.webServer>

</configuration>

0 comments:

Post a Comment