Tschallacka
Tschallacka

Reputation: 28742

Codeigniter controller htaccess redirect

Question: How do I move from an old url to a seo friendly url in codeigniter?

Example: I have a codeigniter controller called library. The URL looks like www.domain.com/library

This is not seo friendly and I want it to look like www.domain.com/keyword-library How do I fix this? .htaccess and routing isn't enough because I need the old pages to redirect to the new url with 301 so searchengines pick this up.

Upvotes: 0

Views: 728

Answers (1)

Tschallacka
Tschallacka

Reputation: 28742

I use the application/config/routes.php to reroute new url queries to the old controller object

$route['keyword-library/(:any)/(:any)/(:any)/(:any)/(:any)'] = "library/$1/$2/$3/$4/$5"; 
$route['keyword-library/(:any)/(:any)/(:any)/(:any)'] = "library/$1/$2/$3/$4"; 
$route['keyword-library/(:any)/(:any)/(:any)'] = "library/$1/$2/$3"; 
$route['keyword-library/(:any)/(:any)'] = "library/$1/$2"; 
$route['keyword-library/(:any)'] = "library/$1"; 
$route['keyword-library'] = "library"; 

Then in the /index.php file before any other code executes I add this to reroute old pages to new pages.

$uri = $_SERVER['REQUEST_URI'];
if(strpos($uri,'/library') === 0)
    {
    $expl = explode('/library',$uri);   
    //array_shift($expl);
    $query = implode('',$expl);
    $redirect= "https://".$_SERVER['HTTP_HOST'].'/orthomoleculaire-bibliotheek'.$query;
    header( "Status: 301 Moved Permanently" );
    header("Location: $redirect");
    exit(0);
    }

I have tried using .htaccess for this, but for some reason I didn't manage to make this work to reroute old urls to new urls with 301 because of the changing variables and variable lengths.

If anyone knows a more elegant solution, please post it, because I haven't been able to find one.

Upvotes: 2

Related Questions