SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parse PHP with .html extension in Apache - best practice?

    Hello!

    I've searched the SitePoint archives and found a few topics, but all are at least 4 years old.

    I have a site where I'd like to continue using the (well established) .html file extensions, but also use php includes for shared elements. I'm very new to PHP and I was wondering whether there's an accepted best practice for having Apache parse PHP within HTML documents.

    http://marcellinosantoso.com/how-to-...in-html-files/ (edit: warning...this site looks like it's having a twitter API problem and is prompting you for a password)

    Googling around, I've found you can either edit the .htaccess file or use the Apache Handlers page in cPanel.

    Are either of these preferred? Are there any downsides to either?

    Thank you!

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    They are the same method, the control panel is just doing the file editing for you. In either case, you're adding an AddHandler directive to your web server configuration telling it to pass .htm/.html files to the PHP interpreter.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, Dan

    Is this a common practice? Will I take a big performance hint this way (significantly more than if the files had a .php extension)?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    None at all, this is how Apache is told to parse .php files as PHP as well

    The actual file extension is completely irrelevant

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excellent. that saves me a lot of potential headache. thanks, dan!

  6. #6
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by liberty79 View Post
    Thanks for the reply, Dan

    Is this a common practice? Will I take a big performance hint this way (significantly more than if the files had a .php extension)?
    Parsing .html files as PHP does not incur a performance penalty vs. parsing .php files as PHP per se. However, the server will parse all HTML documents as PHP, whether they contain PHP code or not. That may or may not cause unacceptable overhead.

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    Parsing .html files as PHP does not incur a performance penalty vs. parsing .php files as PHP per se. However, the server will parse all HTML documents as PHP, whether they contain PHP code or not. That may or may not cause unacceptable overhead.
    Considering that using a PHP CMS or creating a new PHP website is as common, or more common, than writing HTML files these days, it's hard to think of a situation where that would cause unacceptable overhead. It wouldn't with any normal sized website on any standard web host at least.

  8. #8
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Considering that using a PHP CMS or creating a new PHP website is as common, or more common, than writing HTML files these days, it's hard to think of a situation where that would cause unacceptable overhead. It wouldn't with any normal sized website on any standard web host at least.
    It's hard to imagine such a situation, but hard-to-imagine situations tend to arise with frightening frequency when we dismiss them as improbable. It's like some perverse axiom of Murphy's Law.

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doing it through cpanel as described in that link doesn't seem to work

    i set .html and .htm files server-parsed, but <?php ?> stuff shows up as unparsed.

    any ideas?

  10. #10
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't confuse "server-parsed HTML", i.e., Server Side Includes (SSI) with PHP parsing. Ultimately, Apache must be aware that it is supposed to use PHP to parse the files. If CPanel doesn't issue the proper directives to Apache, then it won't work.

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Dont take offence, but you need to stop and start apache to get that working, IIRC.

  12. #12
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Dont take offence, but you need to stop and start apache to get that working, IIRC.
    no offense taken! appreciated, I'll try it out

  13. #13
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Sara System.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I advise against parsing .html files as php, because you open a new territory for many mistakes, you need to remember to check all 3rd party files that have .html extension when uploading to the site in the future (to be sure they don't contain any malicious code), make sure that any modules that allow uploading attachments, files, do not allow uploading .html files.

    Instead you could go with url rewriting, see mod_rewrite in Apache for example. Url may look like: example.com/page.html but the script being executed for that url will be example.com/page.php.

    For example put this in .htaccess:

    RewriteEngine On
    RewriteRule ^page\.html$ /page.php [L]

  14. #14
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BooBooGotU View Post
    I advise against parsing .html files as php, because you open a new territory for many mistakes, you need to remember to check all 3rd party files that have .html extension when uploading to the site in the future (to be sure they don't contain any malicious code), make sure that any modules that allow uploading attachments, files, do not allow uploading .html files.
    interesting. this looks like the preferred method. thanks for the reply


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •