Categories
.htaccess Tutorials & Tips PHP Tips & Tutorials

PHP: Run HTML as PHP

The last time I end up with hundreds of HTML files with PHP codes dispersed in them. Tried to rename them all to .php but failed. To use them and enable the php code within, I have to run the HTML files as PHP.

How Apache web server works in resolving .php requests

Apache gets HTTP requests from client browsers, trys to connect a special file extension of the script requested such as .php to a mime type, in turn affiliates the mime type to a installed module, filter or extension such as PHP and passes the request to it.

In case Apache failed to find the right processor to parse the requested document, it presents the raw content of the file to client which is why we have websites showing the whole bunch of server side code in the browser.

Therefore, now that:

  1. You requests http://www.somesite.com/somescript.php in browser address bar.
  2. Apache detects that the mime type of .php is application/x-httpd-php.
  3. PHP engine is configured to run documents with that mime type.
  4. So it parses it and passes the results to Apache.
  5. Apache sends the results to client.

Pretty much.

Modify .html files as also application/x-httpd-php – the Solution to Run HTML as PHP!

To make PHP engine run .html and the php code in it, you only have to declare a different mime type than .html already is. Find or create a file of .htaccess (there’s a leading dot) in the directory in which you want all .html files to be parsed as .php and append or write the following text:

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

That’s it! Also you may use php5-script instead of x-httpd-php5 in some cases. And you may also try this with regular expressions to better match specific criteria:

<FilesMatch "\.(htm|html|php)$">
SetHandler application/x-httpd-php
</FilesMatch>

Looking to run PHP in HTML code? You mean writing and running PHP code from HTML right?

Categories
.htaccess Tutorials & Tips

12 Most Used .htaccess Examples, Tips – Commands & Directives

Updated: added a 301 redirect directive.

Whatever you wanto call this, a cheat sheet, a real thin manual, or anything, we simplify the use of .htaccess as much as possible and make it do more for you with less hassle.

  1. Unify kavoir.com and www.kavoir.com!
    # Use a 301 redirect from kavoir.com to www.kavoir.com to tell SEs that the 2 domains are the same thing so there's no PageRank leakage.
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^kavoir\.com [NC]
    RewriteRule ^(.*)$ https://www.kavoir.com/$1 [R=301]
  2. Block spammers!
    # This blocks 218.1.132.177 and *.spammers.com from accessing your site.
    <limit GET POST PUT>
    order deny,allow
    deny from 218.1.132.177
    deny from .spammers.com
    allow from all
    </limit>
  3. Change default page!
    # The order is followed as specified:
    DirectoryIndex default.htm default.php index.html index.php
  4. Enable directory browsing!
    Options +Indexes
    # block a few types of files from showing:
    IndexIgnore *.wmv *.mp4 *.avi
  5. Disable directory browsing!
    Options All -Indexes
  6. Customize error messages!
    ErrorDocument 403 /forbidden.html
    ErrorDocument 404 /notfound.html
    ErrorDocument 500 /servererror.html
  7. Get SSI working with HTML/SHTML!
    AddType text/html .html
    AddType text/html .shtml
    AddHandler server-parsed .html
    AddHandler server-parsed .shtml
  8. Redirect it!
    Redirect oldpage.html http://www.domainname.com/newpage.html
  9. Block visits or leeches from specific referers!
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} site-to-block\.com [NC]
    RewriteCond %{HTTP_REFERER} site-to-block-2\.com [NC]
    RewriteRule .* - [F]
  10. Stop a file from being viewed!
    # mycontacts.txt cannot be accessed by any means:
    <files mycontacts.txt>
    order allow,deny
    deny from all
    </files>
  11. Password-protect a directory!
    See http://thejackol.com/scripts/htpasswdgen.php
  12. Change script extensions!
    # anyfile.kv will be treated like a PHP script:
    AddType application/x-httpd-php .kv
    # anyfile.kvcgi will be treated like a CGI script:
    AddType application/x-httpd-cgi .kvcgi