Grid Using SetHandler to process other extensions as PHP


Browse by products and services

  • Applies to: Grid
    • Difficulty: Medium
    • Time Needed: 10
    • Tools Required: FTP, plain text editor or SSH, vi knowledge

Overview

There might be times when you want your webpages to appear like they are html but are actually being processed as PHP. This article briefly describes three ways to accomplish this.

READ ME FIRST

This article is provided as a courtesy. Installing, configuring, and troubleshooting third-party applications is outside the scope of support provided by (mt) Media Temple. Please take a moment to review the Statement of Support.

Method 1 - Process all files with the same extension as PHP

  • Create a .htaccess file in the same directory as the files you'd like to process.
  • Assuming you want all files ending in .html to actually be processed as PHP, insert this line into the .htaccess file you've created:

    Filename: .htaccess

    AddHandler php-script .html
  • Save this file. All html files in that directory will now be processed as PHP.

Method 2 - Process all files, regardless of extension, as PHP

  • This method works well if you have a bunch of files that don't have extensions at all but need to be processed as php. Do not use this method if you have other files in the same directory that shouldn't be run as PHP, such as .jpg, .gif, etc.
  • Create a .htaccess file in the same directory as the files that need to be processed. Put this line into the file:

    Filename: .htaccess

    SetHandler php-script
  • Save it. All files, regardless of name or extension, will now be processed as PHP.

Method 3 - Match certain file names and process them as PHP

  • This method is the most complicated, but will fit specific needs very well. Assume for this example that you'd like any file that has the word 'about' in it to be run as PHP.
  • Create a .htaccess file in the same directory as the files you'd like to be processed as PHP.
  • Use the 'FilesMatch' directive in conjunction with 'SetHandler' to match all files with 'about' in their name:

    Filename: .htaccess

    <FilesMatch "^.*about.*$">
    SetHandler php-script
    </FilesMatch>
  • Save this file. Now any file that includes the word about, such as something-else-about-us.html, will be processed as PHP.

TIP:

More information on these directives can be found at the apache site: http://httpd.apache.org/docs/2.0/mod/core.html.