Accessing .php Pages Without the Need for the .php Extension

Published August 28th, 2013


This article covers setting up your web server to allow access to .php pages without the need for the .php extension. You can also easily modify these examples to work with other extensions as well. This trick is useful for making your URL more friendly and easier to remember. In this post I will show you example code to accomplish this on three different web servers. First I will cover doing so on the Apache web server using a .htaccess file after which an example for Internet Information Services (IIS) using a Web.config file then I will give the code to configure Nginx to do so as well. So let us begin with Apache.

Apache

In order to allow access to .php files without the .php extension in the URL you will first need to ensure mod_rewrite is available. In most cases this module is available. Next we will need to create a file named “.htaccess” if the file does not already exist in the root directory of your site. Now open this file and add the following two lines to it:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]

After adding these lines save the file and place it in the root directory of your web server.

Internet Information Server (IIS)

Accomplishing this for a site being served by IIS is not much harder than it is for Apache. This time you will need to create a file named “Web.config”. The following code is an example of a complete Web.conf file which will allow access to .php files without the need to use the .php extension. Again this file may already exist within the root directory of your site. If it does modify the existing file by adding the following configuration settings within the proper place within the existing Web.config.

<?xml version="1.0"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="rewrite php">
                    <!-- Removes the .php extension for all pages. -->
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" />
                    </conditions>
                    <action type="Rewrite" url="{R:1}.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Nginx

Configuring Nginx to remove the need to use the .php extension is a little more tricky than the others. You will need to be able to edit the server’s configuration file for the site in question directly. You will want to add the line starting try_file from the snippet bellow to your location entry.

location / {
    root /var/www/vhosts/yourdomain.com/httpdocs;
    try_files $uri $uri/ @extensionless-php;
    index index.php index.html;
}

Next add the following lines to your configuration file.

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}

Once done reload Nginx and you should be all set to handle requests without the .php extension.