CodeIgniter Forums
So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? (/thread-20972.html)

Pages: 1 2 3


So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - El Forum - 12-01-2010

[eluser]John_Betong[/eluser]
Once the URL's are all working then check that Google Webmaster Tools is happy with your changes.

Yahoo, Bing, Google, etc all use Canonical Links for the preferred URL.

SEO may also be happier if you set your Canonical Links:
Code:
<head>
  ...
  ...
  <link rel='canonical' href='http://<?php echo base_url(). $this->uri->uri_string(); ?>' />
  ...
  ...
  ...
</head>
 
 
 


So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - El Forum - 03-22-2011

[eluser]k2zs[/eluser]
[quote author="b3nst3wart" date="1254210858"]Finally solved...

First create a "pre-system" hook by adding these lines to your 'config/hooks.php' file:
Code:
$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):
Code:
<?php
function prettyurls() {
    if (is_array($_GET) && count($_GET) == 1 && 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):
Code:
$config['enable_hooks'] = TRUE;

Good luck.[/quote]

OMG That's Fantastic!!!!

I am currently trying to optimize one of my sites for SEO and this worked perfectly! My site only had 4 pages and am trying to expand with more tier 3 pages that use SEO friendly url's. I put this fix in place in no more than 5 min and also used redirects in my old controllers to point to the new url's so that existing links to my site are not broken.

Thank you for providing such a great fix!


So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - El Forum - 03-22-2011

[eluser]InsiteFX[/eluser]
I know you have solved this but here is the Apache .htacces way of doing it!

The Apache .htaccess underscore to hyphen conversion code
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule !\.(html|php)$ - [S=6]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=underscores:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscores:Yes]

RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]

InsiteFX


So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - El Forum - 03-25-2011

[eluser]Unknown[/eluser]
use url_title() for this purpose.


So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - El Forum - 04-27-2011

[eluser]mteejay[/eluser]
Does this work with CI 2.0.2?


So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - El Forum - 05-08-2011

[eluser]jaysonic[/eluser]
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:
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);
}
?&gt;

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


So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - El Forum - 05-11-2011

[eluser]mteejay[/eluser]
I tried this as well and it didn't work. The router code is different in the later version of CI I think.


So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - El Forum - 05-12-2011

[eluser]jaysonic[/eluser]
Which method have you used mteejay? I'm using the latest CI and it's working for me.


So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - El Forum - 08-22-2012

[eluser]Unknown[/eluser]
[quote author="b3nst3wart" date="1254196458"]Finally solved...

First create a "pre-system" hook by adding these lines to your 'config/hooks.php' file:
Code:
$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):
Code:
&lt;?php
function prettyurls() {
    if (is_array($_GET) && count($_GET) == 1 && 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):
Code:
$config['enable_hooks'] = TRUE;

Good luck.[/quote]


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



So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out? - El Forum - 10-09-2012

[eluser]philm[/eluser]
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