views:

3853

answers:

6

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?

A: 

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
    }

  }

?>
Kyle Cronin
+3  A: 

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

Tom Haigh
This is correct. However even when fixed you may face problems, as the CGI standard which defines PATH_INFO requires strings go through a round of URL-decoding, which can break the slash. On Apache only, you can use REQUEST_URI instead which doesn't suffer from this... dunno if CI can use that tho.
bobince
I just fixed this problem on a site. This is a bit of a hack, but it got the job done: I double urlencoded the URL. That way the urldecode of the REQUEST_URI doesn't revert the /'s too early.
Gary Richardson
A: 

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).

Steven Oxley
A: 

Hello Everybody,

A very happy new year.

I have question.

In my codeigniter site I have underscore in URL. Like below

www.example.com/mysite/about_us

I want to chnage this like

www.example.com/mysite/about-us

Basically I want to replace underscore(_) to hyphen(-).

Please help me.

Thanks Riktam

Riktam
A: 

I wants to break my code when some error occure get a template and exit my remaining code go to parsere and parse the data and print my page with error message just like this...

        if($opt==0)
        {                
            $data['Head']        ="Alert !";
            $data['message'] = "Access Forbidden";
            $template = "Notify";
        }
                      ////CODE////
            leave this code
                      ////CODE////

run this -- $this->parser->parse($template, $data);

How to do this..

Huzoor bux
A: 

Change your permitted_uri_chars index in config file

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

Hope this helps!

Cheers

Raheel Khawaja