<p>There's a plugin to set up custom routes: <a href="https://github.com/davist11/craft-reroute" rel="nofollow">Reroute</a> by Trevor Davis, but this would require you to set them all up manually. If there's lots and lots of blog entries, you would probably want to avoid this.</p>

<p>As you said your slugs do match, you could just set up an ElementCriteriaModel with the <code>slug</code> parameter set to the relevant URI segment and see if there's an entry for that; and if that's the case redirect to that entry.</p>

<p>You would do this with this simple PHP code form a custom plugin. <a href="http://buildwithcraft.com/docs/plugins/setting-things-up" rel="nofollow">Set up a plugin</a> and place the snippet in the <code>init()</code> method of your <a href="http://buildwithcraft.com/docs/plugins/setting-things-up#your-primary-plugin-class" rel="nofollow">primary plugin class</a>:</p>

<pre><code>if (craft()-&gt;request-&gt;isSiteRequest())
{
    $inRoot = ((craft()-&gt;request-&gt;getSegment(2) === null) &amp;&amp; (craft()-&gt;request-&gt;getSegment(1) !== null));

    if ($inRoot)
    {
        $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
        $criteria-&gt;section = 'blog';
        $criteria-&gt;slug = craft()-&gt;request-&gt;getSegment(1);
        $entry = $criteria-&gt;first();

        if ($entry)
        {
            craft()-&gt;request-&gt;redirect($entry-&gt;url, true, 301);
        }
    }
}
</code></pre>

CC BY-SA 3.0
2015-05-29T10:36:46.863Z
2015-05-29T10:36:46.863Z
carlcs
<p>I am in the process of migrating a website from Wordpress to Craft and need to redirect the blog entries. In the existing site, the entries are in the root of the site, eg:</p>

<pre><code>example.com/this-is-a-blog-post
</code></pre>

<p>In the Craft site, the post will be under a <code>/blog</code> subdirectory like so:</p>

<pre><code>example.com/blog/this-is-a-blog-post
</code></pre>

<p>Assuming the slugs are the same, how would I go about creating 301 redirects for all existing posts?</p>

<entries><redirect>
How to redirect requests from root to subfolder (if they exist)?
# How to redirect requests from root to subfolder (if they exist)?

(Asked by: username_0 on 2015-05-29)

I am in the process of migrating a website from Wordpress to Craft and need to redirect the blog entries. In the existing site, the entries are in the root of the site, eg:
example.com/this-is-a-blog-post

In the Craft site, the post will be under a /blog subdirectory like so:
example.com/blog/this-is-a-blog-post

Assuming the slugs are the same, how would I go about creating 301 redirects for all existing posts?

**Tags:** entries, redirect

---

## 1 answers

### Answer by username_1 on 2015-05-29. Score: 3 | Accepted answer

There's a plugin to set up custom routes: Reroute by Trevor Davis, but this would require you to set them all up manually. If there's lots and lots of blog entries, you would probably want to avoid this.
As you said your slugs do match, you could just set up an ElementCriteriaModel with the slug parameter set to the relevant URI segment and see if there's an entry for that; and if that's the case redirect to that entry.
You would do this with this simple PHP code form a custom plugin. Set up a plugin and place the snippet in the init() method of your primary plugin class:
if (craft()->request->isSiteRequest())
{
    $inRoot = ((craft()->request->getSegment(2) === null) && (craft()->request->getSegment(1) !== null));

    if ($inRoot)
    {
        $criteria = craft()->elements->getCriteria(ElementType::Entry);
        $criteria->section = 'blog';
        $criteria->slug = craft()->request->getSegment(1);
        $entry = $criteria->first();

        if ($entry)
        {
            craft()->request->redirect($entry->url, true, 301);
        }
    }
}


---

