skip to Main Content

Original URL

mydomain.com/main/portfolio

Now I need to remove the controller name from the URL with the below Route which i have done successfully:

//Controller Name: main
$route['(:any)'] = "main/$1";

My new URL is good:

mydomain.com/portfolio

But the original URL is still accessible which I don’t want for SEO purposes:

mydomain.com/main/portfolio

How can I have my good URL run as normal and block the old URL.

Thanks.

2

Answers


  1. You can create a not found page and redirect unwanted routes to that.
    For instance you can create error controller:

    class Error extends CI_Controller {     
        public function error_404() {
            // send 404 header
            $this->output->set_status_header('404');
            // load a custom not found page view template
            $this->load->view('error_404');
        }        
    }
    

    Then in the routes.php you can catch those URLs which contains the “main/” like this:

    // this should be placed above the (:any) route
    $route['main/(:any)'] = 'error/error_404';
    $route['(:any)'] = "main/$1";
    
    Login or Signup to reply.
  2. RedirectMatch "^/main/(.*)" "http://www.example.com/$1"
    

    More examples available here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search