Redirect () Adds a question mark in front of a URI segment

I recently included enable_query_strings in the CodeIgniter configuration, because when I tried to redirect to something like redirect('/blog?foo=bar') , it removes the GET parameters, but enable_query_strings fixed it.

The problem is that now when I do:

 redirect('/blog'); 

does he add ? to url: http://www.domain.com/?/blog

How to fix it? or how to solve the main problem without including query_strings ?

+4
source share
4 answers

I would recommend using header('location:/blog?foo=bar'); instead of this.

+5
source

Read about what enable_query_strings really does and make sure that it is actually what you want. This is actually just a way to include $_GET .

Confused, I know. Please check out the latest version (currently 2.0.2) and enable allow_get_array configuration allow_get_array . This allows you to support normal $_GET CI support.

enable_query_strings was some kind of strange experimental function, which for some reason is saved in new versions (do people really use it?). This was not and never was a way to use $_GET in normal use, which we all know.

EDIT: It appears that all the helpers of the URL, and all the functions that define your URLs for you, are blocked if you enable this.

In the User Guide enable_query_strings :

Please note: if you use query strings, you will have to build your own URLs, not using the helpers URLs (and other helpers that generate URLs, for example, some of the form helpers), as these are designed to work with Segment Based URLs

So, if you are sure that this is what you want, Karl answer (using vanilla php header for redirection) is pretty much your only hope. Or you can try to provide the full url, because, like base_url() , is probably broken too (?):

 redirect('http://full-urls-are-tedious.com/blog'); 

But it may not even work ...

+2
source

hope this helps! no need to change htaccess, just change your code.

 redirect(base_url()."controller_name/function_name"); 
+2
source

Why are you using query strings? The CodeIgniters router will take care of this for you. You can create a router route that is shorter you want, but the basic structure is this:

 http://www.somedomain.com/controller/function/param1/param2/...etc 

So you can just go to the following:

 http://www.somedomain.com/blog/foo/bar 

or

 http://www.somedomain.com/blog/post/3 

You can also use the router configuration to change it to the following:

 http://www.somedomain.com/blog/3 

You really have to use your CRUD functions that come with it.
http://codeigniter.com/user_guide/general/urls.html
http://codeigniter.com/user_guide/general/routing.html
http://codeigniter.com/user_guide/libraries/uri.html

+1
source

All Articles