.htaccess Tutorials & Tips PHP Tips & Tutorials


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 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

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

.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 and!
    # Use a 301 redirect from to 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 ^(.*)$$1 [R=301]
  2. Block spammers!
    # This blocks and * from accessing your site.
    <limit GET POST PUT>
    order deny,allow
    deny from
    deny from
    allow from all
  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
  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
  11. Password-protect a directory!
  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