Ian Cook
Ian Cook

Reputation:

url encoded forward slashes breaking my codeigniter app

i’m trying to create a url string that works like this:

/app/process/example.com/index.html

so in other words,

/app/process/$URL

i then retrieve the url with

$this->uri->segment(3);

the forward slashes in the URL will of course be a problem accessing uri segments, so i’ll go ahead and url encode the URL portion:

/app/process/example.com%2Findex.html

.. but now I just get a 404 saying ...

Not Found

The requested URL /app/process/example.com/index.html was not found on this server. 

it appears that my url encoding of forward slashes breaks CI’s URI parser.

what can i do to get around this problem?

Upvotes: 13

Views: 16397

Answers (4)

Kyle Cronin
Kyle Cronin

Reputation: 79103

With CodeIgniter, the path of the URL corresponds to a controller, a function in the controller, and the parameters to the function.

Your URL, /app/process/example.com/index.html, would correspond to the app.php controller, the process function inside, and two parameters example.com and index.html:

<?php
class App extends Controller {
    function process($a, $b) {
       // at this point $a is "example.com" and $b is "index.html"
    }
}
?>

edit: In rereading your question, it seems like you want to interpret part of the URL as another URL. To do this, you need to write your function to take a variable number of arguments. To do this, you can use the functions func_num_args and func_get_arg as follows:

<?php
class App extends Controller {
    function process() {
        $url = "";
        for ($i = 0; $i < func_num_args(); $i++) {
            $url .= func_get_arg($i) . "/";
        }

        // $url is now the url in the address
    }
}
?>

Upvotes: 1

Raheel Khawaja
Raheel Khawaja

Reputation: 1

Change your permitted_uri_chars index in config file

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';

Upvotes: 0

Steven Oxley
Steven Oxley

Reputation: 6723

One way to get around this problem would be to replace any forward slashes in your URL variable which you are passing in the URI segments with something that would not break the CodeIgniter URI parser. For example:


$uri = 'example.com/index.html';
$pattern = '"/"';
$new_uri = preg_replace($pattern, '_', $uri);

This will replace all of your forward slashes with underscores. I'm sure it is similar to what you are doing to encode your forward slashes. Then when retrieving the value on the other page simply use something like the following:


$pattern = '/_/';
$old_uri = preg_replace($pattern, '/', $new_uri);

Which will replace all the underscores with forward slashes to get your old URI back. Of course, you'll want to make sure whatever character you use (underscore in this case) will not be present in any of the possible URIs you'll be passing (so you probably won't want to use underscore at all).

Upvotes: 5

Tom Haigh
Tom Haigh

Reputation: 57815

I think the error message you are getting is not from codeigniter but from your web server.

I replicated this using Apache2 without even using CodeIgniter: I created a file index.php, and then accessed index.php/a/b/c - it worked fine. If I then tried to access index.php/a/b/c%2F I got a 404 from Apache.

I solved it by adding to my Apache configuration:

AllowEncodedSlashes On

See the documentation for more information

Once you've done this you might need to fiddle around with $config['permitted_uri_chars'] in codeigniter if it is still not working - you may find the slashes get filtered out

Upvotes: 10

Related Questions