IIS htaccess Rewrite to index.php Subfolder Not Working WordPress

Here is how to internally rewrite all traffic to the index.php using a .htaccess file if you are using an IIS server and your website resides in a subdirectory.

Situation

  • WordPress or other website that needs rewrite to index.php
  • installed in a subfolder
  • IIS server (Microsoft)
  • using permalinks or otherwise requiring requests to be forwarded to index.php
  • using a .htaccess file (and not the alternative web.config)

Goal

Send every request that is not a file or directory to the index.php file of wordpress.

How to Do It

First, I learned that there is not one solution. I have two WordPress installations on the exact same IIS webserver. Both are in a subdirectory. One works with one configuration and the other one requires a different one. That should not be.

The only difference between these two websites is the time I installed them (and the domain mapping). One was installed a few years back and the other one is a fresh install. I do not have access to the server configuration. My conclusion is the following: there are configuration differences on the webserver that I cannot see. And different configurations seem to require different .htaccess files. Or maybe its a software bug in IIS. All that I can do is trying out the combinations.

Here are the combinations to try:

Combination 1

This one works for one of my wordpress sites. There is no RewriteBase statement.

# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^index\.php$ /sub/directory/index.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /sub/directory/index.php [L] </IfModule> # END WordPress

Combination 2

This configuration has a RewriteBase statement. Please note the missing forward slash before index.php. This makes it a relative path. This configuration should also work according to the documentation of the Apache webserver. But I am using IIS and not Apache and it does not work.

# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /sub/directory RewriteRule ^index\.php$ index.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L] </IfModule> # END WordPress

Combination 3

This one works for one of my WordPress sites.
Please note the added forward slash before index.php. This makes it an absolute path.
The RewriteBase is also written as an absolute path.
But wait a second, the /index.php points to the index.php file in the root directory of my hosting and not the index.php file in the subdirectory. I agree with you, but IIS server does not.

# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /sub/directory RewriteRule ^index\.php$ /index.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php [L] </IfModule> # END WordPress

Combination 4 – N

You can try even more combinations. The fields to change are:

  • RewriteBase with the values: “/”, “/sub/directory”, “sub/directory/”, “sub/directory”, “sub/directory/”, no Rewrite base statement at all
  • the target file of the rewrite (second part of the RewriteRule statement, that includes the “index.php”) with the values: “index.php”, “/index.php”, “sub/directory/index.php”, “/sub/directory/index.php”

I found the combination 3 that works for me more by luck. It should not be that way according to documentations I have read in the internet. But that was mostly intended for Apache and not IIS. And IIS might interpret it differently or wrongly. So trying might get you the (unexpected) solution.

Other Explanations

Please not that WordPress needs certain request for a directory to go to that directory and not be rewritten to index.php. That’s why you see the line RewriteCond %{REQUEST_FILENAME} !-d above, if you do not need it, then remove it.

If you cannot get it to work, consider using the alternative web.config file for IIS.

If it still does not work, check whether the webserver supports any rewriting at all (module installed and rewriting permitted?).


Posted on
Last updated on



Leave a Comment
Name (use whatever you like)
E-Mail (optional and not required), will not be published

Refresh
Numbers and letters from picture above (required)