Enable PHP Syntax Highlighting on IIS 7

This post describes how to configure IIS 7 to output syntax highlighted source code for PHP files stored on the web server or site. This feature may be useful for development environments when you want to quickly make the source code accessible to other team members.

WARNING: Never attempt to do what is described below on any internet accessible web site! Doing so will greatly compromise the security of your web application.

The HTTP requests for the PHP source code are usually identified by the “.phps” file extension, e.g. http://localhost/index.phps. You will need to configure IIS to understand and handle the HTTP requests with this extension. There are two options for that: to create an IIS handler mapping for “*.phps” or to use the IIS URL Rewrite Module.

Configuring IIS Handler Mapping

PHP executable supports a command line argument “-s” which is used to display color syntax highlighted source code of any PHP script. You can create a handler mapping for “*.phps” and use “C:\[path to PHP installation]\php-cgi.exe|-s” as the executable, e.g.:

Handler Mapping for PHPS extension

After that you will need to rename or copy the .php file to .phps file and then request it from a web browser.

Using IIS URL Rewrite Module

If you do not want to rename each .php file to .phps, you can use IIS URL Rewrite rule and a simple PHP script (as suggested by Pierre Joye). In the root directory of your web site create a file called highlighter.php and put the following code inside:

if ( isset( $_GET['file'] ) ) {
  highlight_file( $file );

After that add the following rewrite rule to the web.config file located at the root directory of your web site:

  <rule name="PHP Source Code" stopProcessing="true">
   <match url="(.*\.php)s$" />
    <add input="{DOCUMENT_ROOT}\{R:1}" matchType="IsFile" />
   <action type="Rewrite" url="highlighter.php?file={R:1}" appendQueryString="false" />

Now, if you request any url that has an extension .phps the highlighted source code will be returned in the response, provided that the corresponding .php file exists under the web site’s root directory.

Result of requesting a PHPS file

2 thoughts on “Enable PHP Syntax Highlighting on IIS 7”

  1. Pingback: DotNetShoutout

Leave a Reply

Your email address will not be published. Required fields are marked *