Use htaccess to redirect WordPress to static website in a subfolder

If you want an external redirect from /2016/<anything> to /2016/a/<anything>, then you can do something like the following before the existing WordPress directives (ie. before # BEGIN WordPress) in your /2016/.htaccess file:

RewriteCond %{REQUEST_URI} ^/(\d{4})/(.*)
RewriteRule !^a/ /%1/a/%2 [R,L]

The negated RewriteRule pattern prevents the redirect from occurring when the URl-path already starts with a/ (relative to the /2016/ directory). The %1 and %2 backreferences refer back to the matched CondPattern.

Note that since this is an external redirect, the /a subdirectory will naturally be exposed to the user. (If you are internally linking to the /a subdirectory then it’s exposed anyway.)


Alternatively, if you want to internally rewrite all requests from /2016/<anything> to /2016/a/<anything>, and thus hide the /a subdirectory (the user only sees /2016/<anything>) then you can do something like the following instead:

RewriteCond %(ENV:REDIRECT_STATUS) ^$
RewriteRule (.*) a/$1 [L]

The RewriteCond directive that checks against the REDIRECT_STATUS environment variable makes sure that this is only applied to the initial request, not the rewritten request, in order to prevent a rewrite loop.

Note that this rewrite will rewrite the URL regardless of whether /a is already present on the initial request. eg. a request for /2016/a/<something> will be internally rewritten to /2016/a/a/<something> (which I assume would probably result in a 404). But if the purpose is to hide the /a subdirectory then /a shouldn’t be present on the request in the first place.

Again, this must go before the existing WordPress directives. The WordPress site is no longer accessible until these directives are removed.

If URLs of the form /2016/a/<something> have already been indexed, or linked to by external third parties then also consider including a redirect to remove the /a subdirectory from the URL (in order to preserve SEO). For example, the following would need to go before the above rewrite:

RewriteCond %(ENV:REDIRECT_STATUS) ^$
RewriteCond %{REQUEST_URI} ^/(\d{4})
RewriteRule ^a/(.*) /%1/$1 [R,L]

The %1 backreference refers to the directory name ie. “2016” (captured in the preceding CondPattern – this simply saves having to manually hardcode the directory name in the substitution. $1 is a backreference to everything that follows /2016/a/ in the URL-path.


Note that the above “redirects” (indicated by the R flag on the RewriteRule) are temporary (302) redirects. If these are intended to be permanent then change R to R=301, but only after you have confirmed it’s working OK. 301s are cached hard by the browser, so can make testing problematic.