whispersan
whispersan

Reputation: 1039

Codeigniter Prevent Passing Parameter to Function to Avoid Non-Existent URI Segment

So I have a page:

http://www.mysite.com/controller/function

The function is defined in the controller as:

function ()
{
//some stuff here
}

However it is possible to resolve the URL: http://www.mysite.com/controller/function/blablabla

i.e. "blablabla" can be passed to the function and forms an additional URI segment, but still brings up the same page. I have the same issue with a number of controllers / functions - how do I prevent parameters being passed to the function (or appearing as a URI segment)?

I've been working with Codeigniter and PHP for around 6 months very part time, so forgive me if the answer is obvious but my searches haven't been fruitful on this.

My goal is optimised SEO - unsure whether better to redirect the page with the extra URI segment to the correct page or to the 404 page.

Upvotes: 2

Views: 1142

Answers (2)

Kai Qing
Kai Qing

Reputation: 18843

Sounds like you want a generic catch all for pages. You can do this using routes.

For example:

$route['my_happy_function(/:any)*'] = "my_happy_function";

then in your my_happy_function index method you check the URI segments there...

public function index()
{
    $something = $this->uri->segment(1);
    $something_else = $this->uri->segment(2);
    // etc

}

this way all calls to my_happy_function get pushed to the index method...

wait, did I understand your question correctly? If I missed the point let me know and I can update.

Upvotes: 0

WillNewby
WillNewby

Reputation: 46

You can't prevent that without changing how CI handles URI parsing.

You could force a redirect like so:

function my_happy_function($redirect=null) {
   if($redirect) {
   redirect('/controller/my_happy_function/');
   }
}

That would strip out any variables that are given in the URI, at the cost of a page redirect.

Upvotes: 2

Related Questions