Convert Apache .htaccess to IIS Web.Config
A web.config file is the settings and configuration for applications on IIS Server (ex: Azure Paas Websites ). But what if you’re coming from a Linux host – what then?
Well, there are few options:
1) Online Conversion Tool
https://www.htaccesstowebconfig.com/
As always, please verify the converted web.config rules on a development site before introducing them directly into production.
2) IIS Manager
More on IIS Manager .htaccess conversions here: https://blogs.msdn.com/b/azureossds/archive/2015/04/23/converting-apache-htaccess-rules-to-web-config-using-iis-manager-for-azure-and-iis-websites.aspx
3) Manual Conversion
There is a good article on the equivalent components. It can be found here: https://www.iis.net/learn/application-frameworks/install-and-configure-php-applications-on-iis/translate-htaccess-content-to-iis-webconfig
Htaccess Component | web.config equivalent |
FilesMatch Example: <FilesMatch "\.(gif|jpg|^png)$"> Order allow, deny </FilesMatch> |
Example: <security> <requestFiltering>
</requestFiltering> </security> |
Default Document
# Set the default handler. DirectoryIndex index.php |
<defaultDocument> <files> <remove value="index.php" /> <add value="index.php" /> </files> </defaultDocument> |
URL Rewriting
RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] |
<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="https://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> |
Error Page Redirects / Handling
# Make Application handle any 404 errors. ErrorDocument 404 /index.php |
<!-- 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> --> |
Directory Browsing Example:
# Don't show directory listings for URLs which map to a directory. Options -Indexes |
<directoryBrowse enabled="false" /> |