ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

[Solved] Routing issue - need to redirect from url parameterized pages to new CodeIgniter nice urls

November 01, 2009 4:11pm

Subscribe [2]
  • #1 / Nov 01, 2009 4:11pm

    ehicks727

    90 posts

    I’m re-coding an entire site in CodeIgniter.  The current site uses url paramters, such as this…

    http://www.sportsfeatures.com/index.php?section=olympic-article-view&title=London 2012 captured for all time in Portrait Gallery project&id=46145

    For SEO purposes (getting Google to re-index my site over time with the new url structure and avoiding 404s) I’m trying to set up some routing to 301 redirect the old pages to the new pages.

    I’m keying into id=  so if I can get the id of the article into a controller, then I’m good. I’m just having problems getting there and could use some help.

    I’ve tried this..

    $route['index.php?(:any)'] = 'reroute/$1';

    But it comes back with “The URI you submitted has disallowed characters.”

    How can I carve out the id=####  and send it to a controller called reroute?

    Thanks for your help.

  • #2 / Nov 01, 2009 7:57pm

    ehicks727

    90 posts

    Anyone??

  • #3 / Nov 02, 2009 10:06am

    ehicks727

    90 posts

    Ok, after reading a lot of posts on this topic, I’m starting to think that I can’t do this with a CI solution.

    However, can any of you regex gurus help me out with a mod_rewrite solution?  If my incoming url looks like this…

    http://www.sportsfeatures.com/index.php?section=olympic-article-view&title=London 2012 captured for all time in Portrait Gallery project&id=46145

    is there a way I can just capture the ID and send it to my controller?  So in this case, mod_rewrite would change this to…

    http://www.sportsfeatures.com/reroute/story/46145

  • #4 / Nov 02, 2009 10:40am

    BrianDHall

    760 posts

    If you enable query strings in CI (in config.php), and probably set your uri_protocol to PATH_INFO in config.php.

    Then if you have a debugger you can just set a breakpoint in routes.php and you can see that $_GET still exists for your use.

    Then you can just do this in your routes file:

    if ($_GET['id'])
    {
        $route['(:any)'] = 'reroute/story/' . $_GET['id'];
    }

    I tested it on my localhost and it works perfectly. If a ? and somewhere an id= exists, it will trigger the route and away it will go.

    If it gives you problems I like testing my reroutes against a page (function) that has nothing but $this->output->enable_profiler(TRUE);

  • #5 / Nov 02, 2009 10:53am

    Phil Sturgeon

    2889 posts

    You cannot use routes to map sections of your query string. For that, use mod_rewrite in your .htaccess file.

  • #6 / Nov 02, 2009 11:00am

    BrianDHall

    760 posts

    You cannot use routes to map sections of your query string. For that, use mod_rewrite in your .htaccess file.

    Sure you can! *points up*

  • #7 / Nov 02, 2009 12:11pm

    Phil Sturgeon

    2889 posts

    Ha, you dirty b*tch!

    Ok yeah that will work.

    Lemme rephrase: “You cannot use regular expressions or patterns on routes”.

  • #8 / Nov 02, 2009 12:13pm

    BrianDHall

    760 posts

    Ha, you dirty b*tch!

    Ok yeah that will work.

    Lemme rephrase: “You cannot use regular expressions or patterns on routes”.

    Yes, well we can’t prosecute you for that. 😊

    And true, you have to think “outside the array” to accomplish GET-based routing, unless you want to go a hackin’.

  • #9 / Nov 02, 2009 12:49pm

    ehicks727

    90 posts

    If you enable query strings in CI (in config.php), and probably set your uri_protocol to PATH_INFO in config.php.

    Then if you have a debugger you can just set a breakpoint in routes.php and you can see that $_GET still exists for your use.

    Then you can just do this in your routes file:

    if ($_GET['id'])
    {
        $route['(:any)'] = 'reroute/story/' . $_GET['id'];
    }

    I tested it on my localhost and it works perfectly. If a ? and somewhere an id= exists, it will trigger the route and away it will go.

    If it gives you problems I like testing my reroutes against a page (function) that has nothing but $this->output->enable_profiler(TRUE);

    Thank you for replying.. I’m going to give it a shot and I’ll let you know.

  • #10 / Nov 02, 2009 1:04pm

    ehicks727

    90 posts

    Ok, I set to PATH_INFO and enabled query strings, and entered that control statement in my routes file.  It’s just going to the home page now.  It never makes it to my reroute controller.  I just did an echo “HERE”; exit; in that controller and I never get there.

    Should I add some characters to this, or doesn’t that matter?
    $config[‘permitted_uri_chars’] = ‘a-z 0-9~%.:_\-’;

  • #11 / Nov 02, 2009 1:25pm

    ehicks727

    90 posts

    I echoed the $_GET[‘id’] in the routes file, and it is, in fact, finding it… it’s just not routing it… let me try some more debugging.

  • #12 / Nov 02, 2009 1:32pm

    BrianDHall

    760 posts

    I ran into that problem to. I was thinking it wasn’t working, then I closed my browser and opened a new one after clearing cache.

    Suddenly it worked, I presume I had a caching issue of some sort.

    The other issue is the default controller. If you visit the site directly with nothing else defined, it has a tendency to skip routing completely.

    For that you’ll need to use the if($_GET[‘id’]) check on the setting of default controller, and route it appropriately to your rerouted controller.

  • #13 / Nov 02, 2009 1:43pm

    ehicks727

    90 posts

    I actually want to force a server 301 redirect, not just re-route the page… is there any reason I can’t do this in the routes.php file?

    if ($_GET['id']) {
        redirect("/redirectstories/story/".$_GET['id'], 'location', 301);
    }

    It’s not working right now, but I haven’t cleared my cache, like you mentioned above.. I’ll do that next.

  • #14 / Nov 02, 2009 1:48pm

    ehicks727

    90 posts

    For that you’ll need to use the if($_GET[‘id’]) check on the setting of default controller, and route it appropriately to your rerouted controller.


    That worked.. thanks.  I just put this into my default controller and it works great.  Thanks for your help, I really appreciate it!

    if ($_GET['id']) {
        redirect("/redirectstories/story/".$_GET['id'], 'location', 301);
    }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases