<p>Retour <a href="https://nystudio107.com/docs/retour/advanced.html#resolve-redirect-events" rel="nofollow noreferrer">provides some events</a> that you can hook into to provide custom redirect logic. In this case, <code>Redirects::EVENT_AFTER_RESOLVE_REDIRECT</code> is the most suitable event to use. Register an event hook in a custom module like this:</p>
<pre class="lang-php prettyprint-override"><code>use nystudio107\retour\services\Redirects;
use yii\base\Event;

// init()
Event::on(
    Redirects::class,
    Redirects::EVENT_AFTER_RESOLVE_REDIRECT,
    $this-&gt;resolveCustomRedirects(...),
);
</code></pre>
<p>In the callback, you can check if a category for the slug in the path exists and set the <code>redirectDestUrl</code> to that categories' URL:</p>
<pre class="lang-php prettyprint-override"><code>use nystudio107\retour\events\ResolveRedirectEvent;
use craft\elements\Category;

public function resolveCustomRedirects(ResolveRedirectEvent $e)
{
    $path = $e-&gt;pathOnly;

    if (preg_match('%^/([\w\d-]+)/?$%', $path, $matches)) {
        $categorySlug = $matches[1];
        $matchingCategory = Category::find()-&gt;slug($categorySlug)-&gt;one();
        if ($matchingCategory) {
            $e-&gt;redirectDestUrl = $matchingCategory-&gt;getUrl();
        }
    }
}
</code></pre>
<p>The regex might need some work, but you get the general idea. See the linked documentation page for details.</p>

CC BY-SA 4.0
2022-07-21T15:44:53.033Z
Awesome! This is exactly what I needed. Perfect for a jumping off point. Thank you @MoritzLost!
comtyler
CC BY-SA 4.0
2022-07-21T14:57:36.507Z
2022-07-21T14:57:36.507Z
MoritzLost
<p>This is a feature request that some people have asked for in Retour: a way to allow authors to easily create &quot;short URLs&quot; to entries.</p>
<p>I take it that you'd be interested in this as well?</p>
<p>That said, you can accomplish what you want with Retour right now -- it throws events that a custom module could listen for, and do whatever it wants in terms of redirects:</p>
<p><a href="https://nystudio107.com/docs/retour/advanced.html#redirect-events" rel="nofollow noreferrer">https://nystudio107.com/docs/retour/advanced.html#redirect-events</a></p>

CC BY-SA 4.0
2022-07-21T15:43:16.903Z
Yes, "short URLs" is the ultimate goal, and not just for Categories in my instance but for any entry/category/asset with a lengthy URI. Awesome, thanks @andrew.welch for the speedy reply!
comtyler
CC BY-SA 4.0
2022-07-21T14:56:48.420Z
2022-07-21T14:56:48.420Z
andrew.welch
<p>I'm needing a set of redirects that are created dynamically using Categories. I currently have a Category Group and inside of that group I have categories that can be nested 3 levels deep, producing URIs like: <code>/first-category/second-category/third-category</code>. Also of note, each category has its own template.</p>
<p>Here's the use case:</p>
<p>A visitor goes to the following URL:</p>
<p><code>https://www.somedomain.com/third-category</code></p>
<p>but, it is redirected to the actual full URL:</p>
<p><code>https://www.somedomain.com/first-category/second-category/third-category</code></p>
<p>I'm currently using the Retour plugin, but can't quite figure out how to create the redirects dynamically. There are a lot of categories so creating explicit redirects for each category is a bit cumbersome.</p>

<redirect><category><plugin-retour>
Dynamic vanity redirects using custom module or plugin (Retour)?
# Dynamic vanity redirects using custom module or plugin (Retour)?

(Asked by: username_0 on 2022-07-21)

I'm needing a set of redirects that are created dynamically using Categories. I currently have a Category Group and inside of that group I have categories that can be nested 3 levels deep, producing URIs like: `/first-category/second-category/third-category`. Also of note, each category has its own template.

Here's the use case:

A visitor goes to the following URL:

`https://www.somedomain.com/third-category`

but, it is redirected to the actual full URL:

`https://www.somedomain.com/first-category/second-category/third-category`

I'm currently using the Retour plugin, but can't quite figure out how to create the redirects dynamically. There are a lot of categories so creating explicit redirects for each category is a bit cumbersome.

**Tags:** redirect, category, plugin-retour

---

## 2 answers

### Answer by username_1 on 2022-07-21. Score: 0 | Accepted answer

Retour provides some events that you can hook into to provide custom redirect logic. In this case, Redirects::EVENT_AFTER_RESOLVE_REDIRECT is the most suitable event to use. Register an event hook in a custom module like this:
use nystudio107\retour\services\Redirects;
use yii\base\Event;

// init()
Event::on(
    Redirects::class,
    Redirects::EVENT_AFTER_RESOLVE_REDIRECT,
    $this->resolveCustomRedirects(...),
);

In the callback, you can check if a category for the slug in the path exists and set the redirectDestUrl to that categories' URL:
use nystudio107\retour\events\ResolveRedirectEvent;
use craft\elements\Category;

public function resolveCustomRedirects(ResolveRedirectEvent $e)
{
    $path = $e->pathOnly;

    if (preg_match('%^/([\w\d-]+)/?$%', $path, $matches)) {
        $categorySlug = $matches[1];
        $matchingCategory = Category::find()->slug($categorySlug)->one();
        if ($matchingCategory) {
            $e->redirectDestUrl = $matchingCategory->getUrl();
        }
    }
}

The regex might need some work, but you get the general idea. See the linked documentation page for details.

> **Comments:**
> **username_0**: Awesome! This is exactly what I needed. Perfect for a jumping off point. Thank you @MoritzLost!  

---

### Answer by username_2 on 2022-07-21. Score: 0

This is a feature request that some people have asked for in Retour: a way to allow authors to easily create "short URLs" to entries.

I take it that you'd be interested in this as well?

That said, you can accomplish what you want with Retour right now -- it throws events that a custom module could listen for, and do whatever it wants in terms of redirects:

<https://nystudio107.com/docs/retour/advanced.html#redirect-events>

> **Comments:**
> **username_0**: Yes, "short URLs" is the ultimate goal, and not just for Categories in my instance but for any entry/category/asset with a lengthy URI. Awesome, thanks @andrew.welch for the speedy reply!  

---

