Moving WordPress from a sub-domain to a sub-path
Thursday, July 19th, 2018Many years ago, it seemed a good idea to put the company blog to blog.itsth.com. Well, things have changed. SEO und https-certificates now make https://www.itsth.de/en/blog/ look better.
Here’s how to make the transition (assuming the data mentioned above):
- Add this to your .htaccess file:
RewriteEngine on
RewriteRule en/blog/(.*)$ /blog_rewrite.php?$1 - Create a file /blog_rewrite.php with this content:
<?php function HandleAsRewrite($basepath, $blogurl) { $domain = strtolower($_SERVER['HTTP_HOST']); // "www.itsth.de" $scheme = strtolower($_SERVER['REQUEST_SCHEME']); // "https" $input = getenv("REQUEST_URI"); if (substr($input, 0, strlen($basepath))==$basepath) { $page = file_get_contents($blogurl.substr($input, strlen($basepath))); $replacement = $scheme.'://'.$domain.$basepath; $page = str_replace($blogurl, $replacement, $page); $page = str_replace(str_replace('/', '\\/', $blogurl), str_replace('/', '\\/', $replacement), $page); foreach ($http_response_header as $headline) header ($headline); print $page; return(true); } return(false); } if (HandleAsRewrite('/en/blog/', 'http://blog.itsth.com/')) return; print "not found"; ?>
- Optional: Edit the wordpress template’s header file and add:
<link rel=”canonical” href=”https://www.itsth.de/en/blog<?php echo $_SERVER[‘REQUEST_URI’];?>”>
If you want to handle more than one blog, simply duplicate the “if (HandleAsRewrite” and the htaccess-entry.
Limitations: The script won’t handle POST data.