views:

38

answers:

1

My Code Igniter app has urls like this:

http://servername/contexttroot/index.php/Sessions/login/

My question is, when the url is parsed how does Apache know that there isn't a folder called index.php? If I were the parser, that's what I would be looking for :)

I know it works and CI's index.php gets called but I don't understand how this works. Maybe dots aren't allowed as part of directory names?

UPDATE See the comments on the accepted question for details, there are lots of details to what I am asking.

+1  A: 

CodeIgniter basically performs rewrites of the URL by searching for the index file name "index.php" in your URL. Look in CI's config file for this line:

$config['index_page'] = "index.php";

Try changing that to something else and see if it still works.

Edit: OK, to answer the question Juan posed, the way Apache knows that index.php isn't a directory is because a file and directory can't have the same name in the same directory. If index.php were simply a directory, Apache would go into it and search further. If it is a file, it will run the file.

treeface
How can CodeIgniter do a url rewrite? Apache first needs to map it to index.php. Or must you add an entry to http.conf that does the rewriting for CI?
Juan Mendes
If I'm not mistaken, a directory path with a `.` in it is invalid. Again, if I'm not mistaken, an HTTP request will see a `.` as a file extension delimiter and ignore everything after alphernumeric characters (i.e. everything before the first `/`). So it gets up to `index.`, looks for a file extension `php`, sees a `/`, and stops looking for where to go. If you wanted to remove the index.php portion entirely, you'd have to get to the URL in apache's rewrite before it gets to CI.
treeface
That was the only thing I thought made sense, that directories don't allow dots and Apache then decides that it must be the filename. However, I tried http://localhost/dot.dot/test.htm and it worked fine
Juan Mendes
Hmm...try it with dot.php and see if it works (I suspect it will...currently trying to find the real answer)
treeface
Still works, Apache doesn't know about php, so it couldn't have a special case. Here's my guess, Apache splits the url at the slashes and since there can't be a file and a directory with the same name, Apache stops as soon as it encounters something that is a file. Here's a follow up, if that's true, a browser couldn't do that when attempting to resolve relative urls, since it can't test if something is a directory or a file. So from /index.php/Sessions/login, and I had a link to hello.htm, the browser will redirect me to /index.php/Sessions/login/hello/hello.htm.
Juan Mendes
@Juan Actually.........I think I know what it is. I don't think you can have a file and a folder of exactly the same name in the same directory. Because of this, if you have `localhost/index.php/whatever.html`, it will search for a file or folder in localhost called index.php and open/run it. If it's a folder, it will open it and search further. If it's a file, it will run it.
treeface
@Juan I added this to my answer above.
treeface
Any suggestion on the best way to ask my follow up question? How to handle it for relative urls on the client? (See my previous comment)
Juan Mendes
Also, could you update the answer so it doesn't say Code Igniter scans the URL, it's apache that does it, I know the way the question was asked didn't help.
Juan Mendes
@Juan, well the browser isn't doing any of the testing. The browser sends off an http request to the server, the server takes the URL and figures out what to do with it. Apache is doing the check to see if a directory node is a file or directory.
treeface
What I mean is if I navigate to http://server/index.php/session/login and there's a relative link to hello.html, the browser will send a request to http://server/index.php/session/login/hello.html instead of http://server/index.php/hello.html
Juan Mendes
@Juan Is that true? I've used CI on several projects and I've never noticed that behavior. `href="hello.html"` and `href="/hello.html`, I think, will both direct you to server/hello.html. I imagine the browser at least learns the current base path from the server so that it doesn't send `hello.html` to `/session/login/hello.html`.
treeface
Yes it's true, try it with a setup where the index.php is not at the root level. http://server/directory/index/php/Controller/Action. `href='hello.html'` will link to http://server/directory/index/php/Controller/Action/hello.html and `href='/hello.html'` will link to http://server/hello.html. Both cases it would not be what you want.
Juan Mendes