2

Sometimes when I look at a websites URL it will say something like this

www.website.com/index.php

and other times it will look like this

www.website.com/index/

Why does the second URL not have a .php or .html extension?

CatFoodFight
  • 39
  • 1
  • 3
  • 4
    It's commonly called and implemented with "URL rewriting". Google for [`mod_rewrite`](http://stackoverflow.com/tags/mod-rewrite/info) and `RewriteRule`s. Albeit in this case/example it might alternatively just be utilizing `MultiViews` and the `PATH_INFO`. – mario Apr 20 '13 at 01:11
  • 1
    @mario why not post that as an answer? Would perfectly qualify if explained a bit more. – Eduard Luca Apr 20 '13 at 01:12
  • 1
    Using mod_rewrite, all incoming requests can be forwarded to a single PHP processing script, say an index.php, which then interprets the url path and query string, calls other scripts, and generates a dynamic response. Many frameworks work this way, so there's a single entry-point into the application, to enable initialization and configuration without duplicate code all over the place. – kufudo Apr 20 '13 at 01:13
  • 1
    Thank you for the responses! I was able to find a great article that gets into detail about using mod_rewrite. I apologize for being ignorant on the subject and thanks Kufudo for the explanation. – CatFoodFight Apr 20 '13 at 01:21
  • Also, if anyone would like to make an answer for this I will gladly accept it so this topic can be solved. – CatFoodFight Apr 20 '13 at 01:24

1 Answers1

6

In a normally configured web server,

www.website.com/index.php

tells it to load the index.php file from the webroot directory. This

www.website.com/index/

tells it to look in the /index/ directory for a file matching what is listed in the DirectoryIndex configuration directive in the server's httpd.conf file or in an .htaccess file in the web site's webroot directory (see http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryindex for details).

A sample configuration directive would look like this:

DirectoryIndex index.php index.html default.html default.htm

This would tell the web server to, when no filename is given, to look in the current directory for an index.php file, if not found look for an index.html file, and if not found a default.html file and so on for everything else on the line.

If you want to be able to run PHP files without having to have .php on the end of them, you would set a default handler in your configuration files (httpd.conf or .htaccess) such as:

ForceType application/x-httpd-php 

This will tell Apache to handle all files as PHP whether they have a .php extension or not. (See http://httpd.apache.org/docs/2.2/mod/core.html#forcetype )

You can also use ModRewrite to remap the URLs if you prefer, but ModRewrite directives can be confusing and difficult to troubleshoot.

diamondsea
  • 2,980
  • 1
  • 12
  • 9