Moving to php

software development


We are upgrading our website, moving from html pages to php pages.

Is there a way to tell the system: “if someone asks for a page.htm and there is no such page, give them page.php”

I see that I can at least give tham an error page, which I have done. (

I see in the wiki that there is "another option that will also redirect missing .php links…

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ! -f
RewriteCond %{REQUEST_FILENAME} ! -d
RewriteRule (.*) /404.PHP [L]

Can someone please explain to me exactly what that would do, and if I choose to implement it, exactly what do I do in my “root directory” which I assume is the directly above my website directory? or is it the top-most level of the website? and what would the .htaccess file look like? (what’s its file name?)

Sorry to ask such basic questions. I’m out of my depth here.



I’ve numbered the lines for ease of reference. 1 tells Apache to turn on mod_rewrite, which is a module that allows you to do regular expression based search and replace on URLs requested by your users. 2 and 3 check to make sure that what the user asked for is neither a file nor a directory, respectively (in other words, that the user asked for something that doesn’t exist). 4 tells Apache to load and execute 404.PHP from your root directory regardless of what the user asked for. Logically, the way you read that is, “If 2 and 3, then 4,” or in English, “If the user asked for something that is not a file and is not a directory, then run /404.PHP”

Given the above, we can rephrase what you want to tell the system to do as follows: “When the user asks for page.htm, if page.htm is not a file and page.htm is not a directory, then run page.php” I assume, further, that ‘page.htm’ is just an example, and what you really want is any .htm or .html page (*.htm or *.html). This is something regular expressions make easy:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} ! -f RewriteCond %{REQUEST_FILENAME} ! -d RewriteRule ^(.*)\.html?$ /$1.PHP [L] As you can see, the first three lines are the same, just as the rephrased question started out the same. The bottom line says the following: “Start at the beginning of the requested file path and name and look for .htm or .html at the end. If you find .htm or .html at the end, put everything before the dot into $1. Then, invoke the PHP script with the same path and name.”

Note that the parentheses determine what gets thrown into $1 – everything up to but not including the .htm becomes part of $1. To use this method, your PHP script must be at the same place in the filesystem as your HTML was originally.