Enable mod_rewrite in a Ubuntu server - gHacks Tech News

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:

http://192.168.1.100/rewrite.php?link=1

Your users would only have to type:

http://192.168.1.100/link2.html

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:

  1. Instruct Apache to load the mod_rewrite module.
  2. Edit the document root entry so that the rewrite engine is allowed.
  3. 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:

RewriteEngine On
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:

AllowOverride None

to

AllowOverride All

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.

Final thoughts

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.





  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. Rich said on December 6, 2009 at 12:32 am
      Reply

      What you imply – which is not true – is that rewrite rules must go in .htaccess files. If you have access to the main server configuration file, rewrite directives can and should go there. .htaccess files are for people who don’t have access to the main config files.

    2. iceflatline said on December 16, 2009 at 1:59 am
      Reply

      Great article Jack. And perfect timing too. I was in the processing carefully cutting and pasting “LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so” into a rewrite.load file I had created when I thought to myself “there’s got to be a better way…” Sure enough there was!

    3. Newbie said on February 7, 2010 at 11:33 am
      Reply

      Thanks for the great tip. I recently had an issue with enabling the rewrite module on linux mint, and it turns out that the only directive I had missed was changing the ‘AllowOverride None’ to ‘AllowOverride All’.

    4. cYrixmorten said on June 11, 2010 at 10:29 pm
      Reply

      Thanks a lot !!

      I too had trouble enabling .htaccess on linux mint :D

    5. Fokosa said on February 3, 2011 at 10:27 pm
      Reply

      Ty this is purfect! thx for this post :) REP!

    6. ero said on September 4, 2011 at 2:17 pm
      Reply

      Thanks very much. After installing Ubuntu 11.04 I was really in a glitch to make the pretty permalink work in my wordpress blog. It helped a lot after I came here through google :)

    7. Priyank said on December 5, 2011 at 5:26 pm
      Reply

      Thanks a lot..!
      FInally i could successfully setup the rewrite engine correctly..

    8. DVR said on January 29, 2012 at 3:58 pm
      Reply

      Many thanks.. I’m learning and learning more about Linux all the time.
      This was a simple and well explained..

    9. Jessica said on May 25, 2012 at 6:40 am
      Reply

      Great and simple guide. That WORKS!

      Thanks!

    10. hostlift said on June 28, 2012 at 1:58 am
      Reply

      very good post….it really help us to understand the fact. Keep it up.

    11. Jamshid said on August 5, 2012 at 2:03 pm
      Reply

      Thank you very much. It worked for me.

    12. Shraddha said on August 24, 2012 at 3:09 pm
      Reply

      Thanks a ton. God Bless U. Life Saver!

    13. PrettyTrue said on March 3, 2015 at 7:42 pm
      Reply

      Excellent! Even now this is super helpful :)

    Leave a Reply