<p>With the <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#registerSiteRoutes" rel="nofollow"><code>registerSiteRoutes</code></a> hook you can route <a href="http://short.com/e9DG" rel="nofollow">http://short.com/e9DG</a> requests to any template.</p>

<pre><code>public function registerSiteRoutes()
{
    return array(
        'e9DG' =&gt; '/mysection/mytemplate.html',
    );
}
</code></pre>

<p>With <a href="https://buildwithcraft.com/classreference/services/HttpRequestService" rel="nofollow"><code>getUrl</code></a> and <a href="https://buildwithcraft.com/classreference/services/HttpRequestService" rel="nofollow"><code>redirect</code></a> accessible via <a href="https://buildwithcraft.com/classreference/services/HttpRequestService" rel="nofollow"><code>craft()-&gt;request</code></a> you can set up server redirects:</p>

<pre><code>$url = craft()-&gt;request-&gt;getUrl();

if ($url == 'http://short.com/e9DG')
{
    craft()-&gt;request-&gt;redirect('http:/longdomainname.com/longsubpagename', true, 301);
}
</code></pre>

CC BY-SA 3.0
2015-05-25T18:04:57.237Z
2015-05-25T18:04:57.237Z
carlcs
<p>Take a look at <a href="https://github.com/aberkie/craft-shortener" rel="nofollow">Shortener</a> and <a href="https://github.com/davist11/craft-reroute/" rel="nofollow">Reroute</a> for inspiration as well ( might save you some trouble if you're just trying to write a general URL shortener!)</p>

CC BY-SA 3.0
2015-05-26T05:26:21.350Z
2015-05-26T05:26:21.350Z
RitterKnight
<p>I am writing a plugin to that will map <a href="http://short.com/e9DG" rel="nofollow">http://short.com/e9DG</a> to http:/longdomainname.com/longsubpagename</p>

<p>I assume I need to use the config/routes.php but am having struggling to understand how how to set this up. I keep getting 404 as it cannot resolve the url.</p>

<p>As a test I have the foloing code</p>

<pre><code>return array(
    'e9DG' =&gt; array('action' =&gt; '/cocktailrecipes/ingredients/new'),
);
</code></pre>

<plugin-development><routes>
How to route short tag to external web site
# How to route short tag to external web site

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

I am writing a plugin to that will map http://short.com/e9DG to http:/longdomainname.com/longsubpagename
I assume I need to use the config/routes.php but am having struggling to understand how how to set this up. I keep getting 404 as it cannot resolve the url.
As a test I have the foloing code
return array(
    'e9DG' => array('action' => '/cocktailrecipes/ingredients/new'),
);

**Tags:** plugin-development, routes


---

## 2 answers

### Answer by username_1 on 2015-05-25. Score: 3

With the registerSiteRoutes hook you can route http://short.com/e9DG requests to any template.
public function registerSiteRoutes()
{
    return array(
        'e9DG' => '/mysection/mytemplate.html',
    );
}

With getUrl and redirect accessible via craft()->request you can set up server redirects:
$url = craft()->request->getUrl();

if ($url == 'http://short.com/e9DG')
{
    craft()->request->redirect('http:/longdomainname.com/longsubpagename', true, 301);
}


---

### Answer by username_2 on 2015-05-26. Score: 1

Take a look at [Shortener](https://github.com/aberkie/craft-shortener) and [Reroute](https://github.com/davist11/craft-reroute/) for inspiration as well ( might save you some trouble if you're just trying to write a general URL shortener!)


---

