<p>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:</p>

<pre><code>{% 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 %}
</code></pre>

CC BY-SA 4.0
2018-05-03T15:22:42.180Z
2018-05-03T15:22:42.180Z
Ian Ebden
<p>The redirect manager is probably your better bet. I like Retour - <a href="https://github.com/nystudio107/retour" rel="nofollow noreferrer">https://github.com/nystudio107/retour</a></p>

<p>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.</p>

<p>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. </p>

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

CC BY-SA 4.0
2018-05-02T19:52:20.423Z
2018-05-02T19:52:20.423Z
SamC
<p>You can use Yii2 routing </p>

<pre><code>    Event::on(
        UrlManager::class,
        UrlManager::EVENT_REGISTER_SITE_URL_RULES,
        function(RegisterUrlRulesEvent $event) {

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

<p>And in your Controller</p>

<pre><code>public function actionRedirect($route){
    if($entry = Entry::find()-&gt;oldUrl($route)-&gt;one()){ //&lt;- insert your criteria to mach old with new URLs
        return $this-&gt;redirect($entry-&gt;getUrl());
    }
}
</code></pre>

CC BY-SA 4.0
2018-05-02T11:31:52.847Z
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
CC BY-SA 4.0
2018-05-02T11:33:51.350Z
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
CC BY-SA 4.0
2018-05-02T11:06:47.910Z
2018-05-02T11:11:57.927Z
2018-05-02T11:11:57.927Z
Robin Schambach
<p>Working on a Wordpress migration at the moment, and all the post URLs are <a href="http://sitename.com/post-url" rel="nofollow noreferrer">http://sitename.com/post-url</a>, and I need to redirect them to their new home at <a href="http://sitename.com/article/entry-url" rel="nofollow noreferrer">http://sitename.com/article/entry-url</a>. 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?</p>

<craft3><redirect>
Best approach for these redirects
# Best approach for these redirects

(Asked by: username_0 on 2018-05-02)

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?

**Tags:** craft3, redirect


---

## 3 answers

### Answer by username_0 on 2018-05-03. Score: 0 | Accepted answer

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 %}


---

### Answer by username_1 on 2018-05-02. Score: 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.


---

### Answer by username_2 on 2018-05-02. Score: 0

You can use Yii2 routing 
    Event::on(
        UrlManager::class,
        UrlManager::EVENT_REGISTER_SITE_URL_RULES,
        function(RegisterUrlRulesEvent $event) {

        $event->rules[] = [
            'pattern' => '/',
            '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());
    }
}

> **Comments:**
> **username_0**: 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?  
> **username_2**: 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  

---

