How to Set up Mod_Rewrite

Build a better website with this useful tool

Apache's mod_rewrite Takes Some Time to Learn, But the Payoff Is Worth It

The Apache web server is one of the main building blocks of the Internet, serving up websites and applications. It's a complex system with many options, and one of the most important is the Rewrite Module, or "mod_rewrite" for short. In this article, we'll take a look at the uses for mod_rewrite, then explore how to get it set up on your own Apache-based site.

The Purpose of the Apache Rewrite Module

The purpose of this module is rewriting URLs in various useful ways. This means that when users arrive to your website via one URL (either by typing it in or selecting a link containing it), they'll "end up" on another URL. There are a number of reasons you might want to do this.

Redirecting Users

You can use mod_rewrite to direct users from older, outdated pages that may still appear in search results, to newer, up-to-date pages. For example, suppose you have a page at https://www.mysite.com/review2015; if someone comes across this as a link and selects it, you can use a rewrite rule to direct them to http://www.mysite.com/review2019.

Easier to Remember URLs

You've probably seen links related to e-commerce with URLs like https://www.thestore.net/3d78654954c29ace998658. Developers use labels like this to make sure items have a unique identifier, but for non-techie types it's hard to remember.

Fortunately, a re-write rule can help you publicize a nice URL, like https://www.thestore.net/notebooks/spiral/3-subject-spiral/, which, when followed, leads a visitor to the one with the ugly URL.

Unique Page Names

In the beginning days of the Web, it was common to see the actual name of page files when you visited a website. For example, the "About Us" page of a website might be "http://www.someguys.org/about.html."

But there are a couple of reasons you don't see these much anymore. The first is that many websites are run by content management systems, which only have a handful of actual page files. The remainder of the content is added dynamically as the page is served, so all pages might look like they have the same URL, like "http://www.someguys.org/index.php." URL rewriting allows you to have the appearance of these separate pages, while still using a dynamic CMS to manage your website.

More Understandable URLs

Search engines rank pages higher when they have easily understandable URLs. This means it's beneficial for websites to have a URL like "https://www.thecompany.com/services/consulting" instead of "https://www.thecompany.com/index.php?section=services&page=consulting," since the last part looks more like code than a URL.

Rewriting makes sure you're always putting your best face forward, from the URL perspective.

How to Set Up mod_rewrite on Your Site

Using URL rewriting on Apache requires two main components:

  • Enabling the actual rewrite module within Apache
  • Listing the necessary rewrite rules in an .htaccess file.

While the uses for URL rewriting apply equally to other web servers like IIS or nginx, mod_rewrite and the methods below will only apply to Apache.

Enable Rewrite Function in Apache

First, you'll need to turn the rewriting function on in your Apache installation.

  1. You'll need to find where your Apache's configuration file is located. This will depend on the installer you used.

    • On Windows you should look for a directory called "/conf" under the Apache install directory.
    • On macOS's popular XAMPP installer, it's located in /Applications/XAMPP/xamppfiles/apache2/conf.
    • On Linux, it would appear in the /etc/apache2 directory. In any case, you're looking for a file called "httpd.conf," "http.conf," or "apache2.conf," so you can also use the search feature of your OS to look for these.
  2. Open the config file in a text editor, like Windows' Notepad or TextEdit on macOS.

  3. Look for the line "#LoadModule rewrite_module modules/mod_rewrite.so," and remove the hashtag at the beginning. This denotes a comment, meaning when it's there, the Apache server will ignore everything on that line. Remove the hashtag, and next time Apache will execute that line when it starts up.

  4. Save the config file and re-start the Apache server.

  5. Alternately, on some versions of Linux you'll see some directories like "/etc/apache2/mods-available" and "/etc/apache2/mods-enabled." These contain bits of configuration files that are put together dynamically. In this case, you can run the following commands to first enable the rewrite module, then restart the Apache server:

    sudo a2enmod rewrite
    systemctl restart apache2
  6. Now your Apache server is ready to rewrite URLs for you. Only now you need to tell it which ones to rewrite, and under what circumstances.

Defining Mod_Rewrite Rules on Your Site

You'll need to define the rules for mod_rewrite using the regular expression language. Fortunately, there's some help available. For a given rewrite you want to enable, you can use a tool like VisioSpark's rule generator.

  1. Enter the URL you want to rewrite from, i.e. the one that a user will enter, in the From: field.

  2. Enter the URL you want to rewrite to, i.e. the one where the user will end up, in the To: field.

    GUI Rewrite Rule Generators Can Save You the Effort of Learning RegEx
  3. Select the Captcha control, then select Go. The generator should then provide a result starting with "RewriteRule.

If you happen to find another generator these steps might be slightly different, but hopefully in the same ballpark.

Entering Mod_Rewrite Rules for Your Site

So this is great, a point-and-click way to make these rules. But where do you put them?

To use rules on a website, you'll need to create a file called ".htaccess" in the root of the website. It's somewhat platform-dependent where the files for a particular website are stored, but you'll need to have figured that out to deploy your website there in the first place.

There may already be a file with that name. If so, great! You can simply add your lines to that file.

  1. Create a new text file with the name ".htaccess" in the your website's root.

  2. In the first line, enter the below code. This tells the server you want to enable rewriting for the current website.

    RewriteEngine on
  3. Next, add each of the lines you created with the generator. The start of our file would look like this:

    RewriteEngine on
    RewriteRule ^notebooks/spiral/3-subject-spiral/$ /3d78654954c29ace998658?&%{QUERY_STRING}
  4. As with any Apache configuration change, it's a good idea to restart the server.

Using Mod_Rewrite Takes Time, but the Results Are Worth It

Using the mod_rewrite function on Apache definitely takes some effort. You have to fiddle with text-based configuration files, figure out a way to get very finicky regular expressions into those files, and know how to manually stop and restart the service. It'll likely require a lot of trial-and-error before you get something that doesn't result in an error and behaves correctly.

That said, once you get the hang of it, you'll get some great benefit from mod_rewrite. Your visitors will be better able to remember your URLs, and your ranking on Google will likely improve. The resulting "pretty URLs" aren't just for show, after all. They're functional too.