Welcome Guest, Not a member yet? Register   Sign In
Help with CI 3 URL underscores
#1

Hello everyone,

For some reason, a dynamically generated page from my CI project is showing its "title name" wrongly. This example applies to the 2nd generated page in my testing hence the "2". From what I've read, the underscore is bad for SEO.

This is what I'm getting: http://localhost/test/Page_title_2 instead of http://localhost/test/Page-title-2 or even better, http://localhost/test/page-title-2

"$route['translate_uri_dashes']" in application/config/routes.php is actually set to FALSE: Changing it to TRUE does nothing.

What even amazes me is that if I name a page "example_page.php", it will behave well and show as http://localhost/test/example-page. As you can see, the underscore has been rewritten. Obviously this is a page name vs title/heading which I'm actually focused on.

I'd appreciate if anyone could help me find the culprit so I can replace underscores with hyphens for a SEO friendly URL.

Thank you.
Reply
#2

Have you try using url_title() function?
Reply
#3

(04-26-2020, 07:43 PM)seunex Wrote: Have you try using url_title() function?

Hi seunex,

Thanks for your interest. This is what I have in the url_helper file:

if ( ! function_exists('url_title'))
{

function url_title($str, $separator = '-', $lowercase = FALSE)
{
if ($separator === 'dash')
{
$separator = '-';
}
elseif ($separator === 'underscore')
{
$separator = '_';
}

$q_separator = preg_quote($separator, '#');

$trans = array(
'&.+?;' => '',
'[^\w\d _-]' => '',
'\s+' => $separator,
'('.$q_separator.')+' => $separator
);

$str = strip_tags($str);
foreach ($trans as $key => $val)
{
$str = preg_replace('#'.$key.'#i'.(UTF8_ENABLED ? 'u' : ''), $val, $str);
}

if ($lowercase === TRUE)
{
$str = strtolower($str);
}

return trim(trim($str, $separator));
}
}

Is this where the magic is happening?
Reply
#4

No, that's the function you can use to turn Page_title_2 into Page-title-2. You need to find the file that generates the actual link, and use url_title($url, '-', true) to convert them.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB