简体   繁体   中英

CodeIgniter failed when the url does not end with the suffix .html

I am new in CodeIgniter. I setup CI in my localhost Eg. localhost/MyProjects/CodeIgniter/welcome/index

The url_config in my config.php is this:

$config['url_suffix'] = ".html";

When i access the URL eg.

http://localhost/MyProjects/CodeIgniter/welcome/index

or

http://localhost/MyProjects/CodeIgniter/welcome/index.html

the page is loaded.

My question is, how to set CodeIgniter failed when the url does not end with the suffix .html?
So, when i type like this

http://localhost/MyProjects/CodeIgniter/welcome/index

the request will fail or redirect to forbidden

Try use this to redirect it :

$this->load->helper('url'); 
if (preg_match("/.html/i", current_url())) {
    echo "A match was found.";
} else {
    // redirect(base_url());
    show_404(); //if you want to show page not found
}

place inside your function index() in welcome.php controller.

Thanks for your solution, but i give some modification to your solution like this

if (preg_match("/.html/i", $_SERVER['REQUEST_URI'])) {
    echo "A match was found.";
} else {
    // redirect(base_url());
    show_404(); //if you want to show page not found
}

because if I use the current_url() function, then CodeIgniter always provide .html extension that has been set in the $config['url_suffix']='.html'; so I use the $_SERVER['REQUEST_URI'] because it provides what is actually there in the address bar.

Thanks for your advice, my problem was solved. If you are from Indonesia, makasih gan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM