简体   繁体   中英

Make index.php behave as a directory

Some sites (for example, certain implementations of mediawiki,) have a uri format of http://example.com/index.php/SOME_PAGE . How is this done? Is index.php just a directory on the server that happens to be named like a php file and that actually contains a file named SOME_PAGE ? If so, why name it like that? If not (ie it actually does represent some sort of legitimate PHP file), then what would the contents of the look like?

This feature is called path info (see Apache directive AcceptPathInfo ).

When enabled, Apache writes the rest of the requested URI path that's already mapped to an existing file or directory in the PATH_INFO environment variable. So if /index.php/SOME_PAGE is requested and /index.php is an existing file, the request is mapped to that file and the rest of the path after /index.php ( /SOME_PAGE ) is then available in the PATH_INFO variable.

It is not behaving like a directory, it just looks like it is.

index.php is a regular php file.

Here's a tutorial on how to do this with only PHP code. The issue with this is that you are only getting values and you have meaningless key's in your code.

Example from the tutorial, is he is calling index.php/about/Evan/2

To get about from the url, he fetches

$url[0]

That's not very meaningful is it?

Often, it is also done using a .htaccess file and mod_rewrite.

See this article for more info.

Look at the "AcceptPathInfo" directive used in Apache (directly in httpd.conf or via .htaccess). It is a long time since I have used PHP / Apache, and even longer since I did this kind of url prettification, but it is essentially a way of passing extra data into index.php without the "normal" html syntax of ?var1=foo&var2=bar

Note, when I did it, I set up a script called 'dynamic' (or similar) as being the default, PHP parsed handler, and made my URLs look like http://hostname/dynamic/value1/value2

HTH

这些站点使用Web服务器模块(例如apache的mod_rewrite)重写传入的请求,以指向服务器上的实际资源。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM