开发者

Codeigniter first segment URL variables

I am looking to use a URL shortening scheme where I would like the variable to be in the first segment of the URL, www.example.com/0jf08h204. My default controller is "home.php" and I have .htaccess mod-rewrites in place, so what is the best way to manage this? I suppose my smarts have been blocked by the standard /controller/method/variable scheme, is this a URI Protocol setting? Th开发者_如何学Goank you!


To add to Matthew's response.

This is what you'll need in your system/application/config/routes.php file:

$route['(:any)'] = "home";

This will redirect EVERYTHING.

You might not want to redirect everything if you have other controllers which you need to use. If that is the case you can use this regular expression instead:

$route['^(?!about|contact)\S*'] = "home";

This will allow you to redirect everything except the controllers 'about' or 'contact' -- these will be directed to the 'about.php' and 'contact.php' controllers.

Please note, I choose not to use the wild cards within CodeIgniter, you might find they work better for you, I however choose to parse out the $_SERVER['REQUEST_URI'] manually after the redirect. If however you wanted to use the wildcards you would just add /$1 to the routes as you see in Matthew's response.


I myself haven't played a whole lot with Routing, but I think you could try something like this:

$route['(:any)'] = "controllername/actionname/$1";

Haven't tried this myself though.


Well even though it seems a bit non-standard to whitelist certain areas, and I am still running through my head how 404 errors will be handled, here is a method that is working for now using the regex from evolve. My home.php controller file:

class Home extends Controller {

function Home()
{
    parent::Controller();
    $uri = uri_string();
    if(!empty($uri))    {
        $uri_array = explode('/',$uri);
        $first_segment = $uri_array[1];
        if(isset($first_segment))   {
            //do stuff, load alternative view
        }
    }
}

function index()
{
    $this->load->view('home_view');
}

}

/* EOF */

Thanks for the help, please post alternate soln's if available.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜