Rewriting WordPress from a Subdirectory

Running WordPress from a subdirectory keeps a websites root directory clean and allows easy separation from other unrelated content. However, this will result in a longer url e.g. http://ajmm.org/wordpress/ rather than http://ajmm.org/. Fortunately, it is possible to maintain this directory structure yet host WordPress using the shorter root url.

The instructions provided by the WordPress Codex provide one way to do so but the method is more complicated than necessary when considering the rewrite ability of modern webservers. Provided below are rewrite rules for Apache, IIS and nginx which do not require editing any WordPress files.

Note: all configurations assume that WordPress is hosted in a subdirectory called wordpress. Apache and IIS rules should be applied to the root folder only.

Apache .htaccess

<IfModule mod_rewrite.c>

    RewriteEngine On

    # Rewrite empty requests to the wordpress
    # index page rather than trying to display
    # a directory listing.
    RewriteRule ^$ wordpress/index.php [L]

    # Rewrite requests to the wordpress directory
    # when the resource cannot be found.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ wordpress/$1

    # Rewrite requests to the wordpress index page
    # if the resource still cannot be found.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ wordpress/index.php [L]

</IfModule>

IIS web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <directoryBrowse enabled="true" />
    <rewrite>
      <rules>
        <rule name="Wordpress Index Rewrite" stopProcessing="true">
          <match url="^$" ignoreCase="false" />
          <action type="Rewrite" url="wordpress/index.php" />
        </rule>
        <rule name="Wordpress Hard Rewrite" stopProcessing="false">
          <match url="^(.*)$" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="wordpress/{R:1}" />
        </rule>
        <rule name="Wordpress Soft Rewrite" stopProcessing="true">
          <match url="^(.*)$" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="wordpress/index.php" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

nginx

location / {
  try_files $uri $uri/ /wordpress$uri?$args;
}

location /wordpress {
  index index.php;
  try_files $uri $uri/ /wordpress/index.php;
}