问题
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:
- preg_replace the /'s to -'s (or something else) before sending the url then switch it back on the other end.
- Set apache to AllowEncodedSlashes On
- bit of a hack, but you could even save the url to a session variable or something instead of sending through the url *shrug *
- 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