Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Migrating a Site to Silverstripe /

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

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

WP blog has dates in URLs, and SS puts posts in a subdir


Go to End


3 Posts   3556 Views

Avatar
Rhyous

Community Member, 7 Posts

25 November 2010 at 9:28pm

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:
http://site/2010/11/25/some-post

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:
http://site/Myblog/some-post

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)
endif
endif

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

<?php
$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.

Avatar
Ingo

Forum Moderator, 801 Posts

16 December 2010 at 10:04pm

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 :)

Avatar
tractorcow

Community Member, 63 Posts

2 March 2012 at 9:06am

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