简体   繁体   English

redirect() 在 URI 段前添加一个问号

[英]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..我最近在 CodeIgniter 的配置中启用enable_query_strings ,因为当我尝试重定向到类似redirect('/blog?foo=bar')的东西时,它会删除 GET 参数,但enable_query_strings解决了这个问题。

The issue is that now when i do:问题是现在当我这样做时:

redirect('/blog');

it adds a ?它增加了一个? to the url: http://www.domain.com/?/blog到 url: http://www.domain.com/?/blog

How to fix that?如何解决? or how to solve the primary issue without enabling query_strings ?或者如何在不启用query_strings的情况下解决主要问题?

I'd recommend just using header('location:/blog?foo=bar');我建议只使用header('location:/blog?foo=bar'); instead.反而。

hope this will help you, no need to change htaccess.just change your code.希望这会对您有所帮助,无需更改 htaccess。只需更改您的代码。

redirect(base_url()."controller_name/function_name");

Read up on what enable_query_strings actually does and make sure this is actually what you want.阅读enable_query_strings实际作用,并确保这确实是您想要的。 It is not in fact a way to simply enable $_GET .实际上,这不是一种简单地启用$_GET的方法。

Confusing, I know.令人困惑,我知道。 Please check out the latest version (2.0.2 currently) and enable the config option allow_get_array instead.请查看最新版本(当前为 2.0.2)并启用配置选项allow_get_array This allows normal $_GET support to CI.这允许对 CI 的正常$_GET支持。

enable_query_strings was some weird psuedo-experimental feature that persists in new versions for some reason (do people really use it?). enable_query_strings是一些奇怪的伪实验功能,由于某种原因在新版本中仍然存在(人们真的使用它吗?)。 It is not, and never was, a way to use $_GET in the normal usage that we all know.它不是,也从来不是,在我们都知道的正常用法中使用$_GET的一种方式。

EDIT: Looks like all the url helpers, and all the functions that figure out your urls for you are busted if you enable this.编辑:看起来所有的 url 助手,如果你启用它,所有为你找出你的网址的功能都会被破坏。

From the User Guide on enable_query_strings :来自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.请注意:如果您使用查询字符串,则必须构建自己的 URL,而不是使用 URL 帮助程序(以及生成 URL 的其他帮助程序,如一些表单帮助程序),因为这些帮助程序旨在处理基于分段的 URL。

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.因此,如果您确定这是您想要的,那么Karl 的回答(使用 vanilla php 的header进行重定向)几乎是您唯一的希望。 Or, you can try to provide a full URL, seeing as base_url() is probably broken now too (?):或者,您可以尝试提供完整的 URL,因为base_url()现在也可能已损坏(?):

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

But this may not even work...但这甚至可能行不通...

Why are you using query strings?为什么要使用查询字符串? CodeIgniters router will take care of this for you. CodeIgniters 路由器会为你解决这个问题。 You can create a router path that is shorter is you want but the basic structure is this:您可以根据需要创建更短的路由器路径,但基本结构如下:

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

So you could just go to this:所以你可以只 go 到这个:

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

or或者

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

You can also use the router config to change it to something like this:您还可以使用路由器配置将其更改为以下内容:

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

You really should use your CRUD functions that come with it.你真的应该使用它附带的 CRUD 函数。
http://codeigniter.com/user_guide/general/urls.html http://codeigniter.com/user_guide/general/urls.html
http://codeigniter.com/user_guide/general/routing.html http://codeigniter.com/user_guide/general/routing.html
http://codeigniter.com/user_guide/libraries/uri.html http://codeigniter.com/user_guide/libraries/uri.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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