Redirect Old Webpages in Apache with mod_rewrite

Linux, Servers

Sooner or later, you’re probably going to point an old webpage to a new one. If you’ve spent a bunch of time with search engine optimization (SEO), you can retain your work by redirecting the old URL to the new one, and letting search engines know its permanent. Here’s how you can do that with Apache in Linux.

  1. Enable mod_rewrite if it hasn’t been already. Open up a terminal.

    • See which mods are enabled:

      # apache2ctl -M
    • Enable mod_rewrite:

      # a2enmod rewrite
    • Restart Apache

      # /etc/init.d/apache2 restart
  2. Add redirection rules to a .htaccess file in the root of your site

    • Open .htaccess in a text editor. I prefer nano.

      # nano .htaccess
    • Add the following to enable the rewrite engine for the site:

      RewriteEngine On
    • Add a rule to redirect a page permanently (301 means the redirect is permanent).
      The following example redirects alextafoya.com/about/contactme.html to alextafoya.com/contact.php

      Redirect 301 /about/contactme.html http://www.alextafoya.com/contact.php

Happy forwarding!