开发者

Route user to Wordpress in case of a 404 error in codeigniter

开发者 https://www.devze.com 2023-02-11 23:00 出处:网络
I am trying to figure out how to make it so that if a user types in a url and gets a 404 error message, they get routed to wordpress to see if it can figure out the url.

I am trying to figure out how to make it so that if a user types in a url and gets a 404 error message, they get routed to wordpress to see if it can figure out the url.

Currently I have a site xyz.com where it is a wordpress site. I am designing a replacement where xyz.com will be a codeigniter site. I will keep the wordpress part xyz.com/blog will开发者_Python百科 be wordpress.

I would like the old urls that are on search engines to be able to redirect to the blog that still has the same data as before, but is now under /blog.

What would be the best way to accomplish this.

Thanks


This would send any 404s to a custom "/error" controller, along with the original requested uri string:

class MY_Exceptions extends CI_Exceptions{

    function __constructor(){
        parent::CI_Exceptions();
    }

    function show_404($page=''){
        header('Location: /error'.$_SERVER['REQUEST_URI']);
        exit;
    }
}

the controller could then get the URI string, add "/blog/" in front of it, and check if the file exists. If it does, redirect... if not, go to another error page. So in the controller "error":

function index()
{
    $this->load->helper('url');
    // remove "/error" from the uri
    $uri = substr($this->uri->uri_string(),0,6);
    $newuri = base_url() . "blog" . $uri;
    if(file_exists($newuri)):
        redirect($newuri,'location',301);
    else:
        redirect("/anotherErrorPage");
    endif;
}

I hope that helps!

update: originally thought to use htaccess, but since everything in CI goes through, index.php.... that really isn't needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号