0

Working on a Wordpress migration at the moment, and all the post URLs are http://sitename.com/post-url, and I need to redirect them to their new home at http://sitename.com/article/entry-url. It's difficult because there obviously isn't a catch-all URL segment I can use because all posts use first segment. I could go the .htaccess route but there are >2k posts so that would be monstrous. There's a good Redirect Manager plugin where you can set a catch all template and then handle redirect in there, but I'd have to separately write a bunch of Route exceptions for other pages on the site. Anyone think of a good approach?

0

You can use Yii2 routing

    Event::on(
        UrlManager::class,
        UrlManager::EVENT_REGISTER_SITE_URL_RULES,
        function(RegisterUrlRulesEvent $event) {

        $event->rules[] = [
            'pattern' => '/<route:\S+>',
            'route'   => 'controller/to/redirect'
        ];
    }
);

And in your Controller

public function actionRedirect($route){
    if($entry = Entry::find()->oldUrl($route)->one()){ //<- insert your criteria to mach old with new URLs
        return $this->redirect($entry->getUrl());
    }
}
  • Thanks Robin. That's all new to me but I'll take a look. Will I still have to list out all >2k post URLs to match against? – Ian Ebden May 2 '18 at 11:31
  • You have to make sure you can identify the old url with an entry. So you need to store the old post url to the specific entry or you need to make sure the slugs are the same – Robin Schambach May 2 '18 at 11:33
0

The redirect manager is probably your better bet. I like Retour - https://github.com/nystudio107/retour

Retour makes a list of all the urls that get 404 errors (and how many times each gets hit). Then you can click through and say URL A => URL B.

I recently moved a WP site to craft. I exported a sitemap from wordpress, and then clicked through all the pages so they'd show up in the list. Had a spreadsheet of new URLs on another window... And then went to town updating.

Doing a catch-all for redirects, and then writing exceptions to that seems like trouble.

0

Someone else suggested I used the existing 404 template, and do a lookup/redirect. Not tried it yet but I think this should do the trick:

{% set path = craft.app.request.segments|first %}
{% set article = craft.entries.section('articles').slug(path).one() %}
{% if article %}
    {% redirect article.url 301 %}
{% else %}
    Page Not Found
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.