Take the 2-minute tour ×

I am not sure if this belongs here or on Doctype, so I decided to post here.

My question is simple - Most the links on my site, if you were to copy the location right off the main page it would be something like www.example.com/dog/foo

and they return 404's . But if you were to type in www.example.com/dog/foo.php, the proper page would be displayed.

Is there anyway to make it so the .php is added automatically for every page?

Thanks in advance.

share|improve this question

migrated from stackoverflow.com Aug 27 '10 at 1:22

This question came from our site for professional and enthusiast programmers.

Shouldn't you implement some sort of URL Rewriting? –  jakenoble Aug 26 '10 at 22:40

3 Answers 3

You can use the .htaccess file if you are using a apache server. But you don't even need a file that is actually called foo.php. See this example:

RewriteEngine On
RewriteRule nice/link/name$ foo.php?param=value

This would point the link example.com/nice/link/name to the script foo.php in the root dir and even adding a parameter. That is used by many sites using the .htaccess file. For your needs you might use this rewrite rule:

RewriteEngine On
RewriteRule dog/(.*)$ dog/$1.php
share|improve this answer

Another thing that you may be interested in, rather than rewriting the URL, is MultiViews, which will allow you to navigate to example.com/dog/foo.php as example.com/dog/foo. I personally enjoy MultiViews more as it allows you to have things such as example.com/view/8-26-2010/stuff/you/posted where view is the actual php file and /8-26-2010/stuff/you/posted/ is a query passed to it. Anyways, to enable MultiViews put this in your .htaccess

Options Indexes FollowSymLinks Includes MultiViews
share|improve this answer
Actually your second example with example/view/8-26-2010/stuff/you/posted is not entirely accurate. Yes, Apache will find the first file in the URI path that corresponds to the page presented the remaining path is sent as PATH_INFO rather than the query string. The MultiViews just enables it to pick up the file without the extension being provided. –  Jeremy Bouse Aug 27 '10 at 2:56

The option you're looking for in Apache is MultiViews. With this set in your Options either in a .htaccess file or the main Apache configuration file(s) it will tell Apache to find any files with extensions it understands and present it when a more complete filename is not given. This does mandate that the extension (in this case .php) be configured for the proper handler, but if giving the full filename (foo.php) works then that shouldn't be a problem.

share|improve this answer

Your Answer

