Jump to:

329 Posts in 212 Topics by 160 members

Migrating a Site to Silverstripe

SilverStripe Forums » Migrating a Site to Silverstripe » WP blog has dates in URLs, and SS puts posts in a subdir

What you need to know when migrating your existing site to SilverStripe.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2107 Views
  • Rhyous
    Community Member
    7 Posts

    WP blog has dates in URLs, and SS puts posts in a subdir Link to this post

    Hey all,

    I am planning to migrate to SilverStripe and the SilverStripe blog module. It will take time. The SilverStripe devs hooked me up by getting the wordpress importer working.

    My issue:

    I am currently using Wordpress. My WP blog has URLs with dates such as:

    There are a lot of links in Forums and other blogs that point to my blog and I don't want these links to be broken.

    But I imported my WP posts into SS and SS does not have dates in the URL. Instead, SS seems to require that the blog be a subdir of the root, so the same blog on SS is this:

    I want to implement a single piece of code to redirect

    Proposed Solution

    I need to implement the following logic:

    IF PageNotFound
    If Date is in URL (http://site/2010/11/26/some-post)
    replace date portion of url (2010/11/26) with blogname (MyBlog)
    redirect to new URL (http://site/blogname/some-post)

    I have some php code that implements this logic in a rudimentary fashion:

    $blogname = "blog";
    $url = $_SERVER[SCRIPT_NAME];
    $slashcount = substr_count($url, '/');
    if ($slashcount = 4) { // this if statement could be improved maybe using regex
    $url = strrchr($url, "/");
    header('Location: /' . $blogname . $url ) ;


    Anyway, I think this solution would work for me. When someone clicks on a link, they would get the correct blog post.

    What I need help with
    It seems that SS has code to load to = "Page not found".

    If I knew where in the code to load "Page not found" was, I could probably make this change myself. Maybe I could learn something from how "Page not found" is loaded and do some similar saphire/silverstripe logic that is more elegant.

  • Ingo
    Forum Moderator
    801 Posts

    Re: WP blog has dates in URLs, and SS puts posts in a subdir Link to this post

    I think the easiest (and most performant) would be Redirect rules in your .htaccess (mod_rewrite), which is not application specific. You should be able to generate a list of old URLs with a SQL query.

    If you want to get into SilverStripe routing, here's a couple of pointers to get you started:

    in mysite/_config.php

    Director::add_rules(99, array(
    '2010/$Month/$Day/$Slug' => 'MyController',
    '2009/$Month/$Day/$Slug' => 'MyController',
    // ...
    ) ;

    Unfortunately SilverStripe routing doesn't support regexes, so you'll have to hardcode all years in there.

    in mysite/code/MyController.php

    class MyController {
    function handleAction($request) {
    // $request->param('Month') etc. can be used for querying
    // ...

    You could also look into decorating ErrorPage with alternateFilepathForErrorcode() (which in your case would be the new URL rather than the file path). Problem is that you can't overrie the HTTP status code 404, even when the page content is served - not good for SEO.

    BTW: There's a recipe about handling legacy URLs in the SilverStripe book

  • tractorcow
    Community Member
    14 Posts

    Re: WP blog has dates in URLs, and SS puts posts in a subdir Link to this post

    Try this module to see if it fits your needs https://github.com/tractorcow/silverstripe-datelink

    It's not properly documented yet, but you can customise the way that dates are formatted. Check out the comments in /code/DateLink.php

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.