cahyo
cahyo

Reputation: 607

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

Upvotes: 0

Views: 360

Answers (2)

cahyo
cahyo

Reputation: 607

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

Upvotes: 0

Eko Junaidi Salam
Eko Junaidi Salam

Reputation: 1681

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.

Upvotes: 1

Related Questions