Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-25T18:04:57.237Z
Answers.LastActivityDate: 2015-05-25T18:04:57.237Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-26T05:26:21.350Z
Answers.LastActivityDate: 2015-05-26T05:26:21.350Z
Answers.UserDisplayName: RitterKnight
Body: <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>
  
Tags: <plugin-development><routes>
Title: How to route short tag to external web site
ThreadText: # 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`](http://buildwithcraft.com/docs/plugins/hooks-reference#registerSiteRoutes) hook you can route <http://short.com/e9DG> requests to any template.
  
  ```
  public function registerSiteRoutes()
  {
      return array(
          'e9DG' => '/mysection/mytemplate.html',
      );
  }
  
  ```
  
  With [`getUrl`](https://buildwithcraft.com/classreference/services/HttpRequestService) and [`redirect`](https://buildwithcraft.com/classreference/services/HttpRequestService) accessible via [`craft()->request`](https://buildwithcraft.com/classreference/services/HttpRequestService) 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!)
  
  
  ---
  
  