Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, create redirect.php file in the controllers folder (<em>application/controllers</em>) and add this code to this file: <pre><code>if (!defined('BASEPATH')) exit('No direct script access allowed'); class Redirect extends CI_Controller { /** * Method to redirect from an alias to a full URL */ public function index() { $alias = $this-&gt;uri-&gt;segment(1); $this-&gt;db-&gt;select('url'); $query = $this-&gt;db-&gt;get_where('links', array('alias' =&gt; $alias), 1, 0); if ($query-&gt;num_rows() &gt; 0) { foreach ($query-&gt;result() as $row) { $this-&gt;load-&gt;helper('url'); redirect($row-&gt;url, 'refresh', 301); } } else { echo "Sorry, alias '$alias' not found"; } } } </code></pre> <p>Then create table in your database. Your table must be like this:</p> <pre><code>CREATE TABLE IF NOT EXISTS `links` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `alias` varchar(6) CHARACTER SET utf8 DEFAULT NULL, `url` text CHARACTER SET utf8, PRIMARY KEY (`id`), UNIQUE KEY `alias` (`alias`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ; </code></pre> <p>After that, set default controller value to the <em>redirect class</em>. Open <em>application/config/routes.php</em>. Find <code>$route['default_controller']</code> , then set <code>redirect</code> as a value to this variable, like this:</p> <pre><code>$route['default_controller'] = "redirect"; </code></pre> <p>Then enjoy life ;)</p> <p><strong>EDIT:</strong></p> <p>I had forgotten to mention URI routing in <code>config/routes.php</code> for redirecting:</p> <pre><code>$route[':any'] = "redirect/index/$1"; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload