1

I've used Simply Static plugin to create a static copy of my WP website. All the files are in /2016/, and the static copy is in /2016/a/. What should I add to .htaccess in order to make WP redirect from example.com/2016 to example.com/2016/a?

This is my current .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /2016/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /2016/index.php [L]
</IfModule>
# END WordPress
2
  • "to make WP redirect" - It's not really making "WP redirect" is it? Don't you just want to redirect (or rewrite?) all requests to your now "static copy"? Do all your internal URLs include the /a subdirectory? Or are they all relative to current directory?
    – MrWhite
    Commented Apr 13, 2018 at 0:57
  • @MrWhite, you are exactly right. I want to redirect all my requests to the static copy. All the URLs are relative to the current directory. Commented Apr 13, 2018 at 5:39

1 Answer 1

2

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.

2
  • Where exactly to add this code? It doesn't work if I add it at the very beginning, after <IfModule mod_rewrite.c> or after RewriteBase /2016/ Commented Apr 15, 2018 at 10:54
  • 1
    Sorry, I had assumed your .htaccess file was located in the document root, but as you say "All the files are in /2016/" - I've updated my answer. (Incidentally, you don't need the RewriteBase directive or the /2016/ prefix on the RewriteRule substitution if the .htaccess file is located in the /2016/ subdirectory itself, but one or other would be required if the .htaccess file was in the document root.)
    – MrWhite
    Commented Apr 15, 2018 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.