How to remove file extensions from your website page URLs (Apache)

Ever wanted to remove the “.php” from your website’s pages? Say you’re using Apache web server (as most sites do) on a Linux distro. There’s lots of advice out there about how to do this but the vast majority of it focuses on the regex, which isn’t the main problem. So I’m going to go over all of the steps to show you what needs to be done (that worked for me). The main issue, at least for me, was enabling the rewrite module.

There are two ways this could be done. One is to actually do it for the server itself by modifying the Apache server config files, which is what the Apache team recommends on their guide to using htpaccess. The second way is using a .htpaccess file, which is what you would use if your using a shared hosting plan for your website. Using htpaccess is slower than using modified server config files because the server has to search for them and makes a number of system calls in the process, whether or not it finds any. So again I say modify the server config files. However, I’m going to show you the latter version since that’s what you’re probably more interested in anyways (and it’s the one I know how to do).

Step 1: Enable htpaccess reading

By default, Apache doesn’t enable reading the .htpaccess file. You must enable reading by searching for the configuration file.

It seems early versions of Apache (2.2? and earlier) use the label “httpd” for files and directories whereas later versions use the label “apache”. For example, the log files for early versions of Apache are in /var/log/httpd/error_logwhereas for later versions the log files are in /var/log/apache2/error.log . The configuration file for early versions is “httpd.conf” but for later versions it’s “apache2.conf”.

You can find which folder it’s in by following these steps (thanks to the guys at Commanigy) as given below:

$ ps -ef | grep apache

This prints the location of Apache. Then, using the location of Apache (most likely /usr/sbin/apache2), run the following in the terminal:

$ /usr/sbin/apache2 -V

This will print a bunch of things, including something like:

-D SERVER_CONFIG_FILE="/etc/apache2/apache2.conf"

You will need to be root to open this configuration file.

Within the file, you will need to look for the security model, which is given by XML-ish tags named “Directory”. As you can probably guess, these enable access to certain directories. Look for the one where your website contents are stored. It will likely have the path “/var/www/”. This tag and its contents will probably look like this:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted

You will need to change the attribute AllowOverride from “None” to “All”. There are more details on this the Apache website.

Step 2: Add the Rewrite Module

This part is very important and is the part most “fixes” skip.

Within the htpaccess configuration file, you will be using commands of the rewrite module. (I’m inclined to think it may be possible to use the Alias module for this task, but most of the info on the net uses this, and you may want a different regex. I’ll show what that is later.) The module isn’t enabled by default.

The linux program a2enmod is used for enabling the mod we need. You can type this into the terminal and hit “Enter” and it will print a list of modules. For enabling the Rewrite module, type of the following into the terminal (thanks, Jonathan Mayhak):

$ sudo a2enmod rewrite

Then, you need to restart the Apache server, which you can do with the following:

$ sudo service apache2 restart

Step 3: Create the .htpaccess file

Within the directory containing your website (or at least the pages you want to affect), you need to create a file named “.htpaccess”. Yes, that’s it’s full name. It’s possible to name it “.config” or something else if you want to modify the server configuration files, but sticking to the standard is easier.

Within this file, we can insert the following access rules for modifying the incoming URL queries. (Thanks go to the guys at InmotionHosting for their comment (by scott at 2014-07-14).)

RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

This particular set of rules allows you to type in only the name of a PHP web page URL without the file extension. If you try to add the “.php”, it will return a 404 error. The downside of this particular pattern and regex usage is that you can’t have a directory and a file with the same name because the directory is selected first, which causes an error.

There are a number of other patterns out there I saw people try, but the one above is the one that worked for me.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s