Part of the EllisLab Network
   
 
So close with a mod_rewrite that changes underscores to hyphens for SEO. Can anyone close it out?
Posted: 27 July 2009 11:08 AM   [ Ignore ]  
Summer Student
Total Posts:  5
Joined  07-27-2009

Hello,

I’ve been using CI for a while and love it. However, many clients are asking for hyphens in URLs instead of underscores which helps with SEO. I partner with two SEO firms, both of which confirm while having hyphens over underscores is not monumental it does help.

As you know, CI controllers must use underscores as hyphens will not work. I have a mod_rewrite in place that ALMOST works in getting everything converted. Yet the first hyphen in a URL (for the class name) doesn’t work. The mod_rewrite looks like this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ([^-]*)-([^-]*)-([^-]*)-?([^-]*)-?([^-]*)-?([^-]*)-?([^-]*)-?([^-]*)-?([^-]*) $1_$2_$3_$4_$5_$6_$7_$8_$9 [L]
RewriteRule ^(.*?)__+(.*) $1$2 [L]
RewriteCond $1 !^(index\.php|css|emails|favicon\.php|flash|img|p\.php|public|scripts|robots\.txt|tmp)
RewriteRule ^(.*)$ index.php?$1 [L]

The issue is that:

/class-name/

throws a 404 while:

/class-name/function-name/

does not. So even though a class name works fine when the URL contains children segments, it does not when it’s loaded on its own.

If anyone knows how to get CI controllers to behave when hyphens are used instead of underscores, I would GREATLY appreciate the advice.

Thanks in advance for your time.

Profile
 
 
Posted: 27 July 2009 05:45 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  115
Joined  09-07-2008

Does class-name/index work for any of them?

 Signature 

Condos in Saint Louis
Branson Getaway Trips

Profile
 
 
Posted: 27 July 2009 05:49 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  5
Joined  07-27-2009

You mean /class-name/index.php ? No, that also throws a 404.

Profile
 
 
Posted: 27 July 2009 09:18 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  633
Joined  03-21-2009

While I don’t quite understand why hyphens are more SEO friendly than underscores, I wonder if other characters would suffice for your SEO purposes.  If your controllers/functions have underscores you can substitute them with periods or pluses (and probably a few others) in the URL and CI translates automatically, no mod_rewrite needed.  Just a though, maybe it’ll save you some time and server overhead.

Profile
 
 
Posted: 28 July 2009 02:28 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  5
Joined  07-27-2009

Based on what I’ve read, it appears that it doesn’t have a huge impact one way or the other. That said, Matt Cutts (Google software engineer) just recently mentioned (http://www.youtube.com/GoogleWebmasterHelp#play/uploads/81/Q3SFVfDIS5k) that while it doesn’t make much sense to convert a site from underscores to hyphens, it would be best to use hyphens with any new development. Coupled with a client’s request to use hyphens, it’s time to figure out how to get CI to oblige.

Does anyone have suggestions about how to get hyphens to work with CI’s class names using mod_rewrite? What I posted seems to work with any given URL segment except for the class name (first segment).

Also, thanks for the suggestion about using dots or plus signs. While they may save some time/overhead, as you’ve mentioned, they won’t help at all for SEO purposes.

Profile
 
 
Posted: 28 July 2009 03:22 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  596
Joined  09-11-2008

hi if you want to replace _ with - you can use CI route check this user guide

http://codeigniter.com/user_guide/general/routing.html

 Signature 

CI,JQuery,Google Maps

Profile
 
 
Posted: 28 September 2009 09:54 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  2
Joined  03-10-2006

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.

Profile
 
 
Posted: 28 September 2009 10:16 PM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  55
Joined  07-06-2009

Another solution is here, using one small MY_Router.php file in application/libraries/ with an updated function to override the issue at the source:
http://codeigniter.com/forums/viewthread/85901/#623512

Profile
 
 
Posted: 13 October 2009 09:09 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  1
Joined  10-13-2009

b3nst3wart, I’ve been looking all over trying to figure this out. Your solution worked perfectly! Thanks for sharing!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 149971 Total Logged-in Users: 42
Total Topics: 103610 Total Anonymous Users: 4
Total Replies: 518112 Total Guests: 443
Total Posts: 621722    
Members ( View Memberlist )