nav HOME » Web » Moving wordpress to subdir

Moving wordpress to subdir

I had wordpress installed in root directory of domain, but that’s not a very good idea in case you want to have multiple applications on the same domain.

Just moving wodpress to subdir is very easy, but what’s with old links, that are already indexed by search engines? we need to set-up permanent redirects (http 301), so search engines can easily update their indexes.

So, here’s the procedure:

Make backup of everything.

Move files to new subdir.

Change WordPress address (URL) and Blog address (URL) under wordpress general settings to new URL. e.g.: http://www.mydomain.tld/blog

Redirect can be solved with apache mod_alias and regexp negative lookahead. Add this to your apache virtualhost config:

RedirectMatch 301 ^/(?!blog)(.*) http://www.mydomain.tld/blog/$1

If you use permalinks, you may need to remove absolute path from .htaccess file (it’s located in root directory of your wordpress installation by default).

Change:

RewriteRule . /index.php [L]

to

RewriteRule . index.php [L]

Now you just need to update URL’s of uploaded files for existing posts in database. Log-in into MySQL and execute this sql on wordpress database:

update wp_posts set post_content = replace(post_content, 'http://www.mydomain.tld/', 'http://www.mydomain.tld/blog/');

Also, make sure that uploads folder is set to relative path (default settings).

Log-in into wordpress admin interface and check ‘Store uploads in this folder’ value. It should contain this value:

wp-content/uploads

That’s it. When search engines updates their indexes, you can remove RedirectMatch rule. In case you want to add another application to the same domain with active RedirectMatch, just add path to the rule:

RedirectMatch 301 ^/(?!blog|myotherapp)(.*) http://www.mydomain.tld/blog/$1

Posted in Web
Tags:

Leave a Reply