How can I get started with `mod_rewrite` on my Linode running Apache?

Linode Staff

I'm trying to enable mod_rewrite for Apache, but it doesn't seem to be taking. What might my configuration need to look like?

1 Reply

Hey there,

To begin, we have to enable mod_rewrite. After installing Apache by running sudo apt-get install apache2, mod_rewrite can be enabled for Apache on Debian and Ubuntu by running the following command:

sudo a2enmod rewrite

Once you've enabled mod_rewrite, we have to restart Apache to fetch the new configuration. The best way to do this would be to use apachectl, which will helpfully display any errors that might occur on reboot.

sudo apachectl graceful

graceful tells apachectl to please reboot Apache in a nice way, without forcing all child processes to quit immediately.

In our example, we'll be making settings that just modify what requests to example.com/foo look for on the backend. In this case, we'll be having those requests return the contents of the file bar. For our purposes, all the files for example.com will be stored in the following directory:

/var/www/example.com/html

And within that directory, we'll have a file titled bar, which just contains the following line:

<h1>Hello!</h1>

After making bar, we just need to modify the Virtual Host file of our website to setup our rewrite rules. This will be done by modifying the .conf file for our site. In this case, we'll be modifying the file located at /etc/apache2/sites-enabled/example.com.conf.

Within example.com.conf, add the following block:

<Directory "/var/www/example.com/html">
    RewriteEngine On
    RewriteRule "^foo$" "bar"
</Directory>

Let's review what's going on in this block:

  • The Directory directive lets Apache know that the directives enclosed in that block apply specifically to the directory we specify in the opening tag. In this case, our content is in /var/www/example.com/html, so we specify that directory.
  • The RewriteEngine On enables mod_rewrite for the context we're currently working in.
  • Finally, RewriteRule tells Apache how it should be rewriting requests.

There's a bit going on in the RewriteRule directive, so let's break that down as well.

  • The ^ at the beginning of foo notes that this is the beginning of the pattern to match requests against
  • The $ at the end denotes that the pattern is finished. Both of these markers reflect logic following regular expression syntax.
  • Finally, the last string just says to replace anything that matches with that pattern (essentially, any requests for example.com/foo) serve the contents of /var/www/example.com/html/bar.

Now that we've got that taken care of, we just need to restart Apache again.

sudo apachectl graceful

If there weren't any errors, when we send a request to example.com/foo, we should get the contents of bar!

This is just a very simple example of what can be accomplished with mod_rewrite. For more tips and tricks, check out the official documentation, and definitely be sure to check out our handy docs page on the subject: Rewrite URLs with mod_rewrite and Apache.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct