Log in

View Full Version : enabling php in html files with .htaccess



Glen_S
02-26-2007, 09:00 PM
I've been using some very simple php includes to simplify management of some sites I have built. With one site I had already uploaded a large number of .html files so rather than renaming them I added the line "AddType application/x-httpd-php .php .htm .html" to the end of the .htaccess file on the web server. With sites I have built since then I just give all the files a .php extension.

After getting tired of uploading files to my sites to test them, I decided to enable it on my machine so I could test locally. I am an Oracle DBA by day so I already had apache (oracles version) running on my machine, so I installed the latest php and enabled it in apache.

I copied the entire contents & directory structure of two sites to the htdocs folder in the apache directory so that they each have a directory under the htdocs file.

Now, my pages I have built in the newer site with a .php extension render fine, with my menu's that are stored in separate.php files showing up as expected. But with the older site that I added the AddType application/x-httpd-php .php .htm .html line to the .htaccess file the php includes are not showing up, despite me adding the neccesary line to the .htaccess file(s) I added the line first to the .htaccess file in the directory where the pages reside, and when that didnt help added the line to the .htaccess file in the root (htdocs) directory.

What am I missing here?

thanks

djr33
02-26-2007, 10:41 PM
Here's some basic info on this--
http://www.dynamicdrive.com/forums/showthread.php?t=17398

Beyond that, not sure what's wrong. Might be something else about your configuration.

Perhaps locally you are still viewing the .htm as a file and .php as, by default, through the webserver? I dunno. Seems too simple, though.

Glen_S
02-26-2007, 11:02 PM
Here's some basic info on this--
http://www.dynamicdrive.com/forums/showthread.php?t=17398

Beyond that, not sure what's wrong. Might be something else about your configuration.

Perhaps locally you are still viewing the .htm as a file and .php as, by default, through the webserver? I dunno. Seems too simple, though.

Nope, accessing both files through the webserver, ie. http://localhost:7777/<mywebdir>/

Even renaming the pages with a .php extension doesnt do it, I know I'm missing something simple here, just dont know what..

djr33
02-26-2007, 11:08 PM
Hmm.... not sure, then.
Does copying the text from each into a new .php file work?
Maybe there's some lasting ghost trace of their .htm past, so your computer won't read them correctly.

Glen_S
02-26-2007, 11:37 PM
Hmm.... not sure, then.
Does copying the text from each into a new .php file work?
Maybe there's some lasting ghost trace of their .htm past, so your computer won't read them correctly.

Correction - changing the file to a .php extension did work - but I still dont know why the php is not working when they have the .html extension.

I stripped out everything from the .htaccess file so all it has is:

AddType application/x-httpd-php .php .htm .html
AddHandler application/x-httpd-php .html .php .htm

BLiZZaRD
02-26-2007, 11:58 PM
Not sure about local servers, but on my web servers I had to place it OUTside the root dir. One server it didn't work unless it was inside the root, the other it had to be outside.

Worth a shot I guess..

OHH.. one other thought... you said you downloaded the latest version of php... would that be php5? If so your ,htaccess needs to have php5 added to the lines.

Twey
02-26-2007, 11:58 PM
This is a bad idea anyway -- parsing all pages as PHP is a waste of resources.

djr33
02-27-2007, 12:13 AM
Depends on your setup.

Twey
02-27-2007, 12:16 AM
No, parsing a pure HTML page as PHP is always a waste of resources. It may be a waste of resources that the server in question can afford, but it's still a waste of resources.

djr33
02-27-2007, 12:21 AM
Nope.

If you want a site that has .htm files parsed as php, and all of your pages are .htm, and all have php scripting... no waste.
Same would apply to other formats, like .js, .jpg, etc.

Now... each .htm that DOESN'T have php scripting IS a waste, but the amount of waste varies by your setup and how much does have php, etc.

There isn't really a way around it to determine which should be parse and which should not, though. So if you want that, just live with it, I guess.

Glen_S
02-27-2007, 02:13 PM
Not sure about local servers, but on my web servers I had to place it OUTside the root dir. One server it didn't work unless it was inside the root, the other it had to be outside.

Worth a shot I guess..

OHH.. one other thought... you said you downloaded the latest version of php... would that be php5? If so your ,htaccess needs to have php5 added to the lines.

It is php5, so where in the line would you have php5?

like this?
AddType application/x-httpd-php5 .php .htm .html
AddHandler application/x-httpd-php5 .html .php .htm

I tried that, still doesn't parse the php code in the html page.

I'm curious, when you said your .htaccess file would only work outside the root, was it then in the same dir as your pages?

BLiZZaRD
02-27-2007, 10:03 PM
no it was on level with root:

htaccess file
root folder --> folders -->html pages & subfolders

Try adding this to it:



AddHandler php-script .php .php5 .php4 .php3 .html .htm .phtml