Forum Moderators: phranque

Message Too Old, No Replies

change .html to .php

         

J64sqs

2:20 pm on Mar 15, 2005 (gmt 0)

10+ Year Member



I want to change one of my index files from html to php. But for the past couple years, I have linked to the page using [domain.com...] (instead of [domain.com...] So, even though I change it to index.php and then link to it via [domain.com...] won't a lot of visitors get an error page if they have the page bookmarked as index.html? How do I fix this?

Thanks in advance.

encyclo

2:26 pm on Mar 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The trick is not to change the file names at all - but to parse files ending in .html for PHP:

AddHandler application/x-httpd-php .html

No new file names, no redirects, no problem!

sitz

11:43 pm on Mar 15, 2005 (gmt 0)

10+ Year Member



BIG problem; you've now told Apache that *all* HTML files need to be handed to PHP so that the PHP engine can parse the file looking for <?php?> tags. The overhead involved lowers server capacity and is, generally speaking....well, a bit dumb. Sorry. =) (note that the same issue applies when doing something like:

AddHandler server-parsed .shtml .html

...in that every html document will get parsed by Apache looking for SSI tags. Just slows things down.)

See [httpd.apache.org ] for a one way to accomplish this. You could also use mod_rewrite, but this is basic enough that it's not really necessary unless you want the URL in the browser to still be the .html version. If you needed to expand it, so that *all* .html files were rewritten to their .php counterparts, you could use RedirectMatch (or, again, mod_rewrite). I *do* like mod_rewrite, but I prefer not to use it if it's not really necessary; bit like swatting a fly with submarine. =)

J64sqs

2:48 pm on Mar 17, 2005 (gmt 0)

10+ Year Member



Okay, I put the following in my .htaccess file:
redirect 301 /folder/index.html [domain.com...]

But then I kept getting the following error:
Redirection limit for URL exceeded, unable to load the requested page, this maybe caused by blocked cookies.

So in despair, I then deleted the redirect command, and now it works fine on MY computer.

I'm really confused. Can someone explain what is happening?

Longhaired Genius

3:07 pm on Mar 17, 2005 (gmt 0)

10+ Year Member



Rightly or wrongly I use mod_rewrite for this. I use this and it works for me:

Options +FollowSymlinks
RewriteEngine on

# redirects from index.html to index.php
RewriteRule ^directory/index.html$ [mysite.com...] [R=301,L]

I don't think the AddHandler method is so bad. After all, php only has to look at the file and pass it on, that hardly counts as parsing. And what if every page had php code on it anyway?

coopster

12:19 pm on Mar 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I concur. It is not a
BIG problem
at all.


you've now told Apache that *all* HTML files need to be handed to PHP so that the PHP engine can parse the file looking for <?php?> tags. The overhead involved lowers server capacity and is, generally speaking....well, a bit dumb. Sorry. =)

Apology accepted ;)

Please understand that using the AddHandler as described by encyclo allows you to accomplish exactly what you wanted -- parse your server-side technology files. If you are using PHP on all your pages it only makes sense to parse those files accordingly. If you don't you are going to send raw PHP commands to the browser -- ouch.

When you are running a dynamic site, you have to allow server-side processing. The AddHandler method works, and works well on Apache 2.0 and the overhead calls are minimal, at best.

encyclo

1:51 pm on Mar 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps I should offer a comprmise solution? :) If you are only using PHP in one particular .html file, and the others are just plain HTML, you might not want to use the AddHandler method for the whole site - not because of the server overhead (which is minimal), but because passing otherwise static pages through the PHP interpreter removes any caching information, usually handled automatically by Apache for static files.

You can therefore target just one file to be parsed for PHP. Place the following in a .htaccess within the folder which contains the index.html you want parsed:

<Files index.html>
ForceType application/x-httpd-php
</Files>

Still no need for redirects (which are best avoided unless there is really no other way), no significant extra server load, and caching is preserved for the other static HTML files on the site. Will that do? ;)

J64sqs

3:16 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



Thanks for all your suggestions. Actually, the more I think about it, the more I want to start using php includes for easier maintainence. And I've actually been trying to get rid of caching. So, I think I'm going to use the add handler.

Thanks again for all your help.

topr8

3:27 pm on Mar 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i use the add handler method on a site on a shared server,

there is no human detectible slow down in the speed the pages are served.

coopster

4:58 pm on Mar 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




Will that do? ;)

Fair enough compromise for me ;)