Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect() Adds a question mark before the URI segment

I've recently enabled enable_query_strings in CodeIgniter's config because when i tried to redirect to something like redirect('/blog?foo=bar') it removes the GET parameters but enable_query_strings fixed that..

The issue is that now when i do:

redirect('/blog');

it adds a ? to the url: http://www.domain.com/?/blog

How to fix that? or how to solve the primary issue without enabling query_strings?

like image 526
CodeOverload Avatar asked Dec 27 '22 17:12

CodeOverload


2 Answers

I'd recommend just using header('location:/blog?foo=bar'); instead.

like image 112
Karl Laurentius Roos Avatar answered Dec 30 '22 07:12

Karl Laurentius Roos


Read up on what enable_query_strings actually does and make sure this is actually what you want. It is not in fact a way to simply enable $_GET.

Confusing, I know. Please check out the latest version (2.0.2 currently) and enable the config option allow_get_array instead. This allows normal $_GET support to CI.

enable_query_strings was some weird psuedo-experimental feature that persists in new versions for some reason (do people really use it?). It is not, and never was, a way to use $_GET in the normal usage that we all know.

EDIT: Looks like all the url helpers, and all the functions that figure out your urls for you are busted if you enable this.

From the User Guide on enable_query_strings:

Please note: If you are using query strings you will have to build your own URLs, rather than utilizing the URL helpers (and other helpers that generate URLs, like some of the form helpers) as these are designed to work with segment based URLs.

So, if you're sure this is what you want, Karl's answer (using vanilla php's header to redirect) is pretty much your only hope. Or, you can try to provide a full URL, seeing as base_url() is probably broken now too (?):

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

But this may not even work...

like image 40
Wesley Murch Avatar answered Dec 30 '22 05:12

Wesley Murch