Archive for July, 2018

Moving WordPress from a sub-domain to a sub-path

Thursday, July 19th, 2018

Many years ago, it seemed a good idea to put the company blog to Well, things have changed. SEO und https-certificates now make look better.

Here’s how to make the transition (assuming the data mentioned above):

  1. Add this to your .htaccess file:
    RewriteEngine on
    RewriteRule en/blog/(.*)$ /blog_rewrite.php?$1
  2. Create a file /blog_rewrite.php with this content:
    function HandleAsRewrite($basepath, $blogurl)
      $domain = strtolower($_SERVER['HTTP_HOST']); // ""
      $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;
    if (HandleAsRewrite('/en/blog/', ''))
    print "not found";
  3. Optional: Edit the wordpress template’s header file and add:
    <link rel=”canonical” href=”<?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.