CodeIgniter: urlencoded URL in URI segment does not work

流过昼夜 提交于 2019-12-11 10:36:05

问题


I'm trying to put a URL as the value of one of my URI segments in CI. My controller method is defined to accept such an argument. However, when I go to the URL, I get a 404 error. For example:

www.domain.com/foo/urlencoded-url/

Any ideas what's wrong? Should I do this via GET instead?

UPDATE:

// URL that generates 404 http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F

// This is in my profile_manager controller public function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '', $url_current = '')

If I remove the second URI segement, I don't get a 404: http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/


回答1:


It seems that the %2F breaks things for apache.

Possible solutions:

  1. preg_replace the /'s to -'s (or something else) before sending the url then switch it back on the other end.
  2. Set apache to AllowEncodedSlashes On
  3. bit of a hack, but you could even save the url to a session variable or something instead of sending through the url *shrug *
  4. double url encode it before sending



回答2:


Pass urlendode()'d URL in segment and then decode it with own (MY_*) class:

application/core/MY_URI.php:

class MY_URI extends CI_URI {

    function _filter_uri($str)
    {
        return rawurldecode(parent::_filter_uri($str));
    }
}

// EOF



回答3:


You may need to change the rule in config/route.php to accept the encoded characters in URL. Also you can take a look at some of the solution from below articles:

  • http://codeigniter.com/forums/viewthread/81365/
  • http://sholsinger.com/archive/2009/04/passing-email-addresses-in-urls-with-codeigniter/
  • Passing URL in Codeigniter URL segment



回答4:


I actually had to do urlencode(urlencode(urlencode(

and urldecode(urldecode(urldecode(

3 times!! and it finally worked, twice didn't cut it.




回答5:


try

function __autoload($class){
 if(!empty($_SERVER['REQUEST_URI'])){
  $_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_QUERY_STRING'] = $_SERVER['QUERY_STRING'] = $_SERVER['REDIRECT_URL'] = $_SERVER['argv'][0]  = urldecode($_SERVER['REQUEST_URI']); 
}
}

in config.php this method work for me




回答6:


This is very old, but I thought I'd share my solution.

Instead of accepting the parameter as a url path, accept it as a get variable:

http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338?url_current=http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F

and in code:

function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '') {
    $url_current = $this->input->get('url_current');
    ...

This seems to work.



来源:https://stackoverflow.com/questions/10610799/codeigniter-urlencoded-url-in-uri-segment-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!