IIS htaccess Rewrite to index.php Subfolder Not Working WordPress
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
RewriteEngine On RewriteRule ^index\.php$ /sub/directory/index.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /sub/directory/index.php [L] # 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
RewriteEngine On RewriteBase /sub/directory RewriteRule ^index\.php$ index.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L] # 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
RewriteEngine On RewriteBase /sub/directory RewriteRule ^index\.php$ /index.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php [L] # 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?).
Published:
Last modification:
My current infastructure is iis 10 hosted on a windows 2016 server. Any help will be greatly appreciated.
you should be able to do it by placing a .htaccess file in the main directory. The file has to contain the correct code statements, of course.
The .htaccess is a Apache Webserver configuration file to achieve such needs. This file can be used when one does not have access to the main configuration files of the webserver (as in third party hosting). Microsoft IIS also supports it (altough with quirks as my article discusses).
The native way to do it in IIS is a web.config file (again if you do not have access to the main configurations), which can do the same as .htaccess, but is native to IIS.
This article is about how to redirect all traffic that arrives in subfolder to the index.php in that subfolder (the traffic already arrives in the subfolder or a deeper level structure of that subfolder). Your question is slightly different, but the code should look similar if you use htaccess.