Read a .html file as if it were a .php file

Discussion in 'PHP' started by patataur, May 29, 2007.

  1. #1
    Hi :)

    One of my site is done in .html, most of the files.

    I need to insert some php into those files, but i'd like to keep the .html extension so as not to have to rename all my links etc.

    So basically, i want the server to go thru my html pages looking for php code.

    I was told to put the following code into my .htaccess :

    AddType application/x-httpd-php .html 
    Code (markup):
    However it does not work, as when i load a page my browser wants to download a file. Something is wrong.

    Any idea on how to make it work? Is there anything to be done on the server side to enable this? Do i need to change other things in my html pages?

    Thanks for the help.
     
    patataur, May 29, 2007 IP
  2. GMROCKS

    GMROCKS Active Member

    Messages:
    648
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Tou need to be running an apache server to run htaccess.
     
    GMROCKS, May 29, 2007 IP
  3. patataur

    patataur Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i mean, my htaccess is working, it's doing other things like redirect and stuff.
     
    patataur, May 29, 2007 IP
  4. gandaliter

    gandaliter Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What file does your browser try to download? In the past I have had some problems like this which usually turn out to be related to incorrect permissions on files.
     
    gandaliter, May 29, 2007 IP
  5. Fold

    Fold Peon

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Some servers aren't set up to allow HTML files to process PHP, even if you instruct them as such in your .htaccess file. It's worth contacting your host to check this before you tear too much hair out trying to make it work.
     
    Fold, May 29, 2007 IP
  6. gandaliter

    gandaliter Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    In Dreamweaver, if you rename a file, it asks you if you want to update links in other files, this could be just what you need. Also, if you don't use Dreamweaver, you could get a free trial from Adobe's web site. I don't know if there are any other tools out there which do this.
     
    gandaliter, May 29, 2007 IP
  7. patataur

    patataur Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    it actually is the page html file, which is named something like 145gtht35 somehow! when i open it in notepad it turns out to be my page code source.

    oups and i don't have much to spare by the way! :D thanks guys for the help
     
    patataur, May 29, 2007 IP
  8. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #8
    you have to change the extension of the page to php if you want to use PHP in the script.php won't work in a HTML script if it is not saved as a PHP file.but you use html extension to access this php file by using mode_rewrite option.you just have to set the moderewrite engine on and add new rewriterules.there are so many tutorials available online for mod_rewrite works.just search google and get the result for mod_rewrite.
     
    coderbari, May 29, 2007 IP
  9. patataur

    patataur Peon

    Messages:
    389
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #9
    not true, it can be done with a htaccess if the server allows it, by telling the server to parse the html file looking for php.

    i know how to do that, thanks. however if all my internal links link to a .php extension, i'd rather then have a .php than a .html

    so if i rename my files in .php i will not mod_rewrite into .html
    i fear SEOwise if i change from .html to .php , as i have some good results with my .html pages. i'm number one in my field on a number of keywords and i'd like to be sure it'll remain so if my .html is changed in .php

    anyway, it appears my hoster won't let me use that
    AddType application/x-httpd-php .html
    Code (markup):
    it is on a shared server, must be the reason why
     
    patataur, May 29, 2007 IP
  10. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    That addtype command will work on some shared hosts, apparently not yours. If you want to use php in what was once an html file while preserving links, mod_rewrite will do the trick. Just rename each file.html to file.html.php and then add the following rule to your .htaccess:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*\.html)$ $1.php
    Code (markup):
    This rule will cotinue to serve a.html as normal, unless a.html.php exists, in which case that is served instead.
     
    lemaitre, May 29, 2007 IP