Skip to main content

How to use .htaccess files on Windows Server IIS

Learn how to use .htaccess in Windows Server IIS. In this post I’ll provide you with some useful .htaccess URL rewrite examples. URL rewrite examples that you can use on Window Server IIS for your website.

URL Rewriting in IIS

Before IIS 7 and the URL Rewrite module, you had to rely on third party extensions to IIS for URL rewriting with .htaccess files. One example of such an extension is the widely used ISAPI_Rewite 3 by Helicon Tech. Helicon ISAPI_Rewrite is superseded by Helicon Ape nowadays.

Here is some information for you to use .htaccess in IIS.

Using such an extension made search engine friendly URL’s with WordPress Permalinks or Joomla! SEF possible, and some other neat stuff like protecting your website from 0-day exploits or to hiding the .php extension with URL Rewrite Module & Helicon Ape. Emulating Apache MultiViews on IIS, both web.config and .htaccess examples in that post! :-)

Since IIS 7 and the IIS URL Rewrite module, you can add rewrite rules to your website’s web.config file. You can convert your old Apache .htaccess to web.config manually or automatically.

However you can always install and use Helicon Ape on an IIS web servers to process .htaccess files. This makes your website more cross-platform.

About Helicon Ape

Apache emulator for Microsoft IIS. It literally implements Apache configuration model (like .htaccess and httpd.conf files) and all most demanded Apache modules in a single IIS add-on, not only making IIS compatible with Apache, but also extending it’s functionality by a number of highly essential features. You can check all currently available modules in the compatibility chart (the list is growing with new builds).

Helicon Ape is implemented as managed IIS 7 (7.5) module, but can be installed as .NET module on IIS 6 as well. It works transparently for both server and client and can even be installed on a shared hosting account without administrative access.

Includes following modules: mod_rewrite, mod_proxy, mod_auth, mod_gzip, mod_headers, mod_cache, mod_expires, mod_replace and others.

ISAPI_Rewrite 3 comes as an ISAPI Filter and Helicon Ape as a managed IIS 7 module which can be installed as a .NET module. Unfortunately the ISAPI_Rewrite 3 and Helicon Ape syntax are not always compatible with each other, and therefore you should only use Helicon Ape nowadays.

I thought you might find this interesting:   Hide .php extension with IIS URL Rewrite Module (how-to)

IIS URL Rewrite

IIS URL Rewrite 2.0 enables Web administrators to create powerful rules to implement URLs that are easier for users to remember and easier for search engines to find. By using rule templates, rewrite maps, .NET providers, and other functionality integrated into IIS Manager, Web administrators can easily set up rules to define URL rewriting behavior based on HTTP headers, HTTP response or request headers, IIS server variables, and even complex programmatic rules.

In addition, Web administrators can perform redirects, send custom responses, or stop HTTP requests based on the logic expressed in the rewrite rules.

All .htaccess examples are for Helicon Ape.

.htaccess examples for IIS

The .htaccess examples I describe are:

WordPress Permalinks .htaccess

Helicon Ape recognizes existing directory and existing file as test strings for %{REQUEST_FILENAME}. By negating we turn this around which makes the rules only processed if a request_filename is not an existing file or directory. All those requests are then rewritten to WordPress’ index.php file for internal handling.

# Enable the RewriteEngine.
RewriteEngine On

# When the HTTP request doesn't match and existing file 
# or directory, Rewrite the request and send it to the 
# index.php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

And some would still say search engine friendly URL’s are not possible on IIS with .htaccess files… sigh

Rewrite linked domains to one master web site – how to map multiple domains 1

Most hosting providers offer the ability to map multiple domains to one master domain and/or webspace.

I thought you might find this interesting:   KB3157663: Cumulative Update for Windows Server 2016 Technical Preview 5

Let’s say you have example.com, example.net, and example.org hosts your website. You’d like to have all requests for www.example.com, example.com, www.example.net, example.net to be rewritten to www.example.org, how would you accomplish that?

Of course you can list all domain names in your .htaccess file, but that doesn’t scale easily. Therefore, you can map all incoming HTTP request, except for www.example.org, to your main website on www.example.org.

You can choose to rewrite the requests, but for Google Search Engine Optimization (SEO), and duplicate content, it’s better to permanently redirect them with a 301 redirect. And here is how!

Add to your .htaccess:

# Enable the RewriteEngine.
RewriteEngine On

# Rewrite the HTTP request for every mapped domain to the 
# master domain. Replace "example.com" with your master 
# domain name.
RewriteCond %{HTTP:Host} ^(?!www\.example\.com)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http://www\.example\.com%1 [L,R=301]

This one also takes care of the REQUEST_URI, meaning that a request for www.example.net/foobar.php will be redirected to www.example.org/foobar.php.

Linked domains to a subfolder on the master web site – how to map multiple domains 2

The same as above, we want to map multiple domain names to our master domain. But now map them to a subfolder, for example for different languages: example.nl, example.de, example.fr, example.co.uk.

All requests are automatically mapped to a sub-folder with the same name as the domain name. Be aware of that!

# Enable the RewriteEngine.
RewriteEngine On

# Rewrite a HTTP request for a mapped domain to a subfolder, 
# with the name of that domain.
#
# Replace example.nl with your domain name, on the first 
# and third line
RewriteCond %{HTTP:Host} ^(?:www.)?example\.nl$
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? example%1 [L]

We can probably make this more robust:

# Enable the RewriteEngine.
RewriteEngine On

# assuming the domain is always example, with several ccTLD's.
# We list all ccTLD's for convenience.
RewriteCond %{HTTP:Host} ^(?:www)?\.example\.(nl|be|de|co\.uk|fr)$
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? %2%3 [L]

This should map example.nl to example.org/nl

Manage and rewrite sub-domains on IIS with Helicon Ape

As with the above .htaccess to map a linked domain to a subfolder, we can do the same for subdomains. A subdomain substitutes www in the address bar, sales.example.com for example.

# Enable the RewriteEngine.
RewriteEngine On

# Rewrite any subdomain to a folder with the same name, 
# except for www. Nothing is rewritten if no subdomain 
# is used.
# Subdomains like "www.foo" are rewritten to "/foo",
# "foo.bar" to "/foo.bar".
RewriteCond %{HTTP:Host} ^((?!www.)(.+)|(?:www.)(.+)).(example\.com)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? %2%3%5 [L]

How to rewrite your non-www domain to www (canonical hostnames, example.com to www.example.com)

In order to not to display double content – which is bad for your Google SEO – we need to rewrite non-www URL’s to www. Assuming we want to have our website available on www.example.com. Again we use a 301 permanent redirect.

I thought you might find this interesting:   Add websites and application pools to IIS 7.5, 8.0 with PowerShell, in a for loop

Again, REQUEST_URI is included too.

# .htaccess IIS rewrite
# Enable the RewriteEngine
RewriteEngine On

# Rewrite any HTTP request for a domain name without 
# www to www.domain name.
RewriteCond %{HTTP_HOST} ^(?!www.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http://www.%1%2 [L,R=301]

Block all visitors, except from some IP addresses

Sometimes you want or need to block access to all visitors, except from your own IP address. Fortunately Helicon Ape supports the REMOTE_ADDR server variable, which you can use to test the visitors IP address against.

Blocked visitors simply receive a Forbidden error message.

# Enable the RewriteEngine.
RewriteEngine On

# Deny access to everyone (all visitors), except the IP addresses 
# below. Change 11.222.33.444 and 555.66.7.888 with the 
# IP addresses.
#
# You can use this .htaccess file on IIS in separate folders 
# to protect/secure them.
RewriteCond %{REMOTE_ADDR} !(11.222.33.444|555.66.7.888)
RewriteRule .* / [F,L]

There are several use cases for blocking visitors with Helicon Ape. For example when you’re creating your own .htaccess HTTP blacklist with Helicon Ape, or if you need to temporarily disable your website for maintenace.

Please share this post if you found it useful, thank you! If you have a valuable tip, please let me know and drop me a comment.

Jan Reilink

My name is Jan. I am not a hacker, coder, developer, programmer or guru. I am merely a system administrator, doing my daily thing at Vevida in the Netherlands. With over 15 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization.

3 thoughts to “How to use .htaccess files on Windows Server IIS”

  1. Hi Jan

    These are excellent examples you have provided. I’m new to this and was wondering how would i use RewriteProxy in the following situation?

    I have a website hosted by myself (www.mydomain.com), a wordpress blog hosted by another company (blog.mydomain.com), however how do i proxy the wordpress content when i type http://www.mydomain.com/blog/ in my site?

    I’m using ISAPI_Rewrite 3 from Helicon with IIS6

    Your help will be greatly appreciated!!




    0



    0
  2. Hi Andy,

    Thank you for your comment. Something like the following should work:

    # Enable the RewriteEngine.
    RewriteEngine on
    
    # Rewrite hostheader www.mydomain.com/blog/ through to 
    # blog.mydomain.com/ using a RewriteProxy request.
    #
    # Change example.com with your domainname and aaa.bb.ccc.d 
    # with the IP address of webserver B.
    #
    RewriteCond %{HTTP:Host} ^(?:www.)mydomain.com$
    RewriteCond %{REQUEST_URI} /blog(.+)
    RewriteProxy .? http://aaa.bb.ccc.d/%1 [H]

    You do need to know and use the IP address of blog.mydomain.com in the RewriteProxy rule. But are you sure you want to proxy? Why not rewrite or redirect from http://www.mydomain.com/blog/ to blog.mydomain.com?

    Note: provided as-is, not tested.
    Edit: misread original question, edited comment accordingly




    0



    0

Hi! Join the discussion, leave a reply!