EllisLab text mark
Advanced Search
2 of 2
2
   
So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out?
Posted: 11 May 2011 10:52 PM   [ Ignore ]   [ # 16 ]   [ Rating: 0 ]
Joined: 2011-04-27
7 posts

I tried this as well and it didn’t work.  The router code is different in the later version of CI I think.

 
Posted: 12 May 2011 05:05 AM   [ Ignore ]   [ # 17 ]   [ Rating: 0 ]
Joined: 2011-05-08
2 posts

Which method have you used mteejay? I’m using the latest CI and it’s working for me.

 
Posted: 27 October 2011 12:10 AM   [ Ignore ]   [ # 18 ]   [ Rating: 0 ]
Joined: 2009-11-06
46 posts
jaysonic - 08 May 2011 07:31 AM

Another solution, which has been posted although it modifies all the segments, is as follows.

Create the MY_Router.php file in /application/core and in it place the following code:

<?php
public function _set_request($segments){
    
// Fix only the first 2 segments
    
for($i 0$i 2; ++$i){
        
if(isset($segments[$i])){
            $segments[$i] 
str_replace('-''_'$segments[$i]);
        
}
    }
    
    
// Run the original _set_request method, passing it our updated segments.
    
parent::_set_request($segments);
}
?> 

This just modifies the first 2 segments in your URL, but only if they are set. Hopefully that helps someone.

why don’t you just do

parent::_set_requeststr_replace'-''_'$segments ) ); 

?

Anyway, this solution works only because CI is not written in true OOP manner. The code hasn’t completely been upgraded from the PHP 4 days. If you look at the source code of the CI_Router class, the _set_request function is supposed to have private access; that is, no other class—not even the classes that inherit from it—is supposed to access it or override it. If in the next release they decide to put “private” in front of it, then your upgrade will not be easy, especially if this is not the only modification of this kind.

I know, a little late, but I suggest the hooks method.

 
Posted: 22 August 2012 09:30 AM   [ Ignore ]   [ # 19 ]   [ Rating: 0 ]
Joined: 2011-09-25
2 posts
b3nst3wart - 28 September 2009 11:54 PM

Finally solved…

First create a “pre-system” hook by adding these lines to your ‘config/hooks.php’ file:

$hook['pre_system'= array(
    
'class'    => '',
    
'function' => 'prettyurls',
    
'filename' => 'myhooks.php',
    
'filepath' => 'hooks',
    
'params'   => array()
); 

Now create a ‘myhooks.php’ file within the ‘application/hooks’ folder and add this function (don’t forget to open a PHP tag if this is the first hook):

<?php
function prettyurls() {
    
if (is_array($_GET) && count($_GET) == && trim(key($_GET), '/') != ''{
        $newkey 
str_replace('-','_',key($_GET));
        
$_GET[$newkey] $_GET[key($_GET)];
        unset(
$_GET[key($_GET)]);
    
}
    
if (isset($_SERVER['PATH_INFO'])) $_SERVER['PATH_INFO'str_replace('-','_',$_SERVER['PATH_INFO']);
    if (isset(
$_SERVER['QUERY_STRING'])) $_SERVER['QUERY_STRING'str_replace('-','_',$_SERVER['QUERY_STRING']);
    if (isset(
$_SERVER['ORIG_PATH_INFO'])) $_SERVER['ORIG_PATH_INFO'str_replace('-','_',$_SERVER['ORIG_PATH_INFO']);
    if (isset(
$_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'str_replace('-','_',$_SERVER['REQUEST_URI']);

You will probably need to edit your ‘config/config.php’ file to enable hooks (around line 91 for me):

$config['enable_hooks'TRUE

Good luck.


Actually , this is the best solution. Recommend 100%.

 

 
Posted: 09 October 2012 07:35 AM   [ Ignore ]   [ # 20 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-19
28 posts

And don’t forget the last step… after you’ve updated your app with the new config file and wonder why it’s stopped working, doh! smile

 
2 of 2
2