4

We have a blog on a subdomain which needs to be moved to the main domain

  • ie: blog.example.com/post-title
  • to example.com/blog/post-title

Do I need to create a redirect match for each individual post/blog or can I do a broad match via .htaccess?

Currently the two are in separate directories as different WordPress installs. The subdomain will fall away eventually because we are consolidating.

1

Currently the two are in separate directories...

Since the subdomain and main domain point to different subdirectories, the required redirect is the same as if on completely different domains/servers.

You just need a single mod_alias Redirect in the .htaccess file in the root of your blog subdomain.

  1. Delete the existing directives in the .htaccess (otherwise, the WordPress front-controller and any other mod_rewrite directives will still be processed).

  2. Add the following mod_alias Redirect:

    Redirect 302 / https://example.com/blog/
    

Change the 302 (temporary) redirect to 301 (permanent) only when you are sure it's working OK.

The Redirect directive is prefix-matching. Everything after the match is appended onto the end of the target URL. eg. blog.example.com/post-title is redirected to example.com/blog/post-title.


UPDATE:

The subdomain will fall away eventually because we are consolidating.

You'll need to keep the subdomain in place for "as long as possible". Obviously, without the subdomain you can't redirect - there is no alternative to preserve SEO and usability. You will need to keep the subdomain in place at least until the new site has been indexed and the subdomain has dropped from the SERPs - which could take a considerable time, depending on the size and age of the old site. Backlinks could take much longer and may never get updated.

You don't need to keep the old site and you don't necessarily need the old subdomain to point to the original location. You could change the subdomain so that it points to the root of the new site and incorporate the redirect as part of your canoncial redirect. So the overhead of maintaining the subdomain is minimal. For example, using mod_rewrite at the top of the .htaccess file on your new WordPress site:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule (.*) https://example.com/$1 [R=301,L]

This redirects any request that isn't for your canonical host (which includes any subdomains and www) and redirects to your canonical host. If your .htaccess file is inside the /blog subdirectory then you will need to modify the RewriteRule substitution to read: https://example.com/blog/$1.

Note that this redirect (on the new site) needs to be implemented using mod_rewrite, as opposed to mod_alias (Redirect), since your new WordPress site is most probably already using mod_rewrite.

There maybe some complications if you are planning on implementing HSTS.

  • This is a perfect answer although, we plan to shut down the blog. subdomain. – Matthew Murdoch Sep 21 '18 at 13:53
  • You can't do anything if you shut down the blog subdomain, so you will need to keep it active for "as long as possble". I've updated my answer. – MrWhite Sep 21 '18 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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