Page 1 of 1

remove index.php from url (http://domain.com/?dir=/etc )

Posted: Thu Nov 29, 2007 4:33 pm
by szalinski
hello everyone

i am trying to modify a directory indexing script and i would like to know if there is a way to 'remove' the /index.php?file= format type from a url so there is just /?file= (e.g. a question mark after a slash which automatically points to an index.php file.

i would like to do this without using a .htaccess url rewrite, i don't see the point of it, because i want to make my own from 'scratch'. therefor when i say 'remove' index.php from the url, i really mean how to code the script in such a way that the index.php is never shown in the url (or the title of a link on the page, etc.)

if so, can it be done for a php script of anyfilename (e.g. not just index.php)?

please help, thanks (i've looked everywhere on google and can't find anything... :( )

Posted: Thu Nov 29, 2007 5:25 pm
by aaronhall
/?foo=bar is equivalent to /index.php?foo=bar -- just make sure your links use the former format. You can add a couple lines to the top of index.php to check $_SERVER['REQUEST_URI'] and redirect if index.php is in the uri

Posted: Thu Nov 29, 2007 5:29 pm
by szalinski
aaronhall wrote:/?foo=bar is equivalent to /index.php?foo=bar -- just make sure your links use the former format. You can add a couple lines to the top of index.php to check $_SERVER['REQUEST_URI'] and redirect if index.php is in the uri
i know it's equivalent, but let's say i open a directory in a folder (for example /files), will it show in the url as http://domain.com/?dir=files/ after it is open, or will it show as http://domain.com/index.php?dir=files (i want the former)

8O

Posted: Thu Nov 29, 2007 8:44 pm
by Jonah Bron
I'm really confused on what you are talking about, szalinski. Could you clarify?

Posted: Thu Nov 29, 2007 9:10 pm
by szalinski
what i mean is that if you download any common directory indexer out there (e.g. PHP AutoIndexer) you will find that if you look in the addressbar as you browse directories, the url is always http://localhost/AutoIndex/index.php?dir=.
Also, if you look on forums or other things, there is usually in the url, the name index.php at the end of it, esp. when you are looking at a particular item, where some variable would need to be taken from the url e.g. /index.php?download=2 for example.

the truth is i just want to know how on some sites they are able to allow a user to click on links on the site that have the index.php removed, such as http://localhost/?dir=/myfiles/. The only example of type of site that makes extensive use of a file called 'index.php' is a directory indexer, it's all i can think of to make it clear what i want to do! Take note on how there is no index.php needed in the previous url there.
now i know that this address is the same as saying http://localhost/index.php?dir=/myfiles/. Obviously it would depend on how the script is written on what gets passed to the index.php file, so it may say something other than 'dir' after it, i'm just giving an example in which case the index.php text appears in the url.

what i want to do is for someone to explain to me how a script can be written so that all my urls that point to

Code: Select all

http://localhost/index.php?variable_here
can be shown as

Code: Select all

http://localhost/?variable_here
. In other words i do not need or want to show index.php at the end of the url. It just looks neater in my opinion. I'm also curious on how to do it because all I come across are scripts that seem to point to $_SERVER[PHP_SELF] (or whatever it is).

hope you can understand me! :)

Posted: Fri Nov 30, 2007 3:18 am
by Mordred
szalinski wrote: what i want to do is for someone to explain to me how a script can be written so that all my urls that point to

Code: Select all

http://localhost/index.php?variable_here
can be shown as

Code: Select all

http://localhost/?variable_here
. In other words i do not need or want to show index.php at the end of the url. It just looks neater in my opinion. I'm also curious on how to do it because all I come across are scripts that seem to point to $_SERVER[PHP_SELF] (or whatever it is).

hope you can understand me! :)
This is not a matter of how the script is written, but how the web works. EVERY url referring to a default index page can be written in that way. It is your job to output links in exactly the same format though.

Posted: Fri Nov 30, 2007 9:33 am
by Jonah Bron
Or if it is a form, the action must be set to http://domain.com/, and not http://domain.com/index.php

Posted: Fri Nov 30, 2007 10:31 am
by szalinski
Believe it or not, I think I am beginning to understand the concept. :lol:

What you as PHP experts, are explaining to me, is as difficult for me to understand as it is for me to explain myself to you.

If i am not mistaken, are you saying that there is no such thing as 'removing' index.php via PHP? This might shock you, but I already know that you can't remove index.php from a url using some exotic PHP coding; it's simply a matter of how you write the code in order that when a user clicks on a link it points to something like the format /?file= as opposed to something like /index.php?file= (?)!
So you are probably wondering now, "So what is it you don't understand?"...
Well, what I find confusing is the $_SERVER['PHP_SELF'] variable. I think the only reason it was put in something like a directory indexing script is so that a user can rename the script (since it only consists of one .php file usually) to whatever they wish, eg directory.php. Then the script would still be able to work because it had the script filename included. :)