Enable mod_rewrite in a Ubuntu server
If you have done much work with Apache then most likely you have experienced a need for enabling the rewrite engine. If you are unfamiliar with the rewrite engine, you can think of it as a means for your web server to literally rewrite urls so they are easier for users to remember. So instead of having to enter:
Your users would only have to type:
Of course the rewrite engine serves as much more than just a means for your users to more easily remember addresses. Many server softwares (such as the social networking server Elgg) depending upon the rewrite engine in order to function. Because of this, the rewrite engine is almost a necessity to have working. By default Apache does not have the rewrite engine on, so you have to configure it to work. The goal of this tutorial is to show you how to enable the rewrite engine in a Ubuntu server installation. This article will assume you already have your Ubuntu server as well as Apache up and running.
What this involves
In order to get the rewrite engine working for your web site you will have to take care of the following steps:
- Instruct Apache to load the mod_rewrite module.
- Edit the document root entry so that the rewrite engine is allowed.
- Create an .htaccess file to rewrite urls for you.
Creating a simple test
Before we get to the actual configuration, the first thing to do is to set up a little rewrite test. Open up a terminal window (or log into your GUI-less server) and change to the /var/www directory. In that directory create a file, named rewrite.php, with the following contents. NOTE: Code is located on pastebin.com for ease of use.
Copy and paste the contents of that file into your rewrite.php file. If you visit that page, in a browser, without the rewrite engine working, you will quickly see that it will not work. So, let's get rewrite working.
Now you will have to create an .htaccess file in the /var/www/ directory with the following contents:
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
Enabling the module
In order to enable the mod_rewrite module in the Ubuntu server issue the following command:
sudo a2enmod rewrite
The above Apache2 Enable Module command will add the correct line in the /etc/apache2/apache2.conf file. That is the only change you need to make with the apache2.conf file. Now it's time to make a change to the document root.
In older versions of Apache all virtual host directory directives were managed in the /etc/apache2/apache2.conf file. This has changed. Now these alterations are handled within the /etc/apache2/sites-enabled/ directory. Within that directory you will find, by default, a single file called 000-default. If you open that file up for editing you will see, at the top, the two sections you need to edit in order to enable the rewrite engine for the document root of your Apache server.
First look in the <Directory /> section and change the line:
Do the same for the <Directory /var/www/> section.
Once you have the file edited, restart Apache with the command:
sudo /etc/init.d/apache2 restart
Now revisit the rewrite.php page in your browser and you should see that rewriting is now working. Congratulations, you have just taken on step forward in opening up an entire world for your Apache needs.
The rewrite engine is used by so many web-based tools. Upon installation of Apache, this should be one of the first tasks you tackle.Advertisement