September 19, 2010 (14 years ago)

How to Redirect Webpages to a New Domain using Module Mod_Rewrite of Apache Server 2.0.46 with .htaccess Files


updated 9.19.2010

Goal:
Configure the Apache Server to redirect webpages to a corresponding page in the new domain address.

The nice thing about url rewriting is you get to keep your old web site traffic when you use it for redirecting users to a new domain name. No need to even notify subscribers of the new website address. Bookmarks and search engine results bearing the old url will still be able to direct visitors to the new address before they even realize the change.

Sample Demonstration

Click on the sample links below.

Notice how the original domain, audreycreation.com.ph in the url changes to www.audreycreation.com.ph while retaining the rest of the original url.

Enable and Load Module module_rewrite in Apache Server

As a prerequisite, we must load the mod_rewrite module first during Apache web server startup.

  1. Open your Apache configuration file with Notepad or Wordpad. The easiest way to access it is through the Apache program group: Start > All Programs > Apache HTTP Server 2.0.46 > Configure Apache Server > Edit the Apache httpd.conf Configuration File.
  2. Find (Ctrl+F) “mod_rewrite”. Uncomment the following line:
    #LoadModule rewrite_module modules/mod_rewrite.so
    To uncomment, remove the # sign that preceedes it. This enables the rewrite module mod_rewrite during Apache server startup or restart.
  3. Find (Ctrl+F) “.htaccess”. Change the word “None” in the following line to All:
    AllowOverride None
    This allows files with filename .htaccess to control the rewrite module with directives for the directory and/or subdirectories that contains it.
  4. Save the configuration file.
  5. Restart Apache server. The recommended way is through the Apache program group: Start > All Programs > Apache HTTP Server 2.0.46 > Control Apache Server > Monitor Apache Servers. R-click the Apache icon in the system tray > Open Apache Monitor. Click Restart button. If the log says:
    The Apache2 service has restarted.
    Your Apache configuration file was set correctly. Otherwise, check your configuration file for typo errors specially lines with quotation marks. Make sure to replace them with the keyboard quotation mark key.

Create the .htaccess File

Let's now test create the .htaccess file and configure module_rewrite through there.

  1. Open Notepad or your preferred plain text editor. In Windows, the easiest way to do this is by clicking Start > Run... > type notepad > OK
  2. Copy and paste the following rewrite directives:
    RewriteEngine on
    RewriteRule ^(.+) http://www.newdomain.com/$1 [R=301,L]
    RewriteRule ^ http://www.newdomain.com/ [R=301,L]
    Replace “www.newdomain.com” with your new domain address.

    The flag R=301 indicates a 301 status code, or permanent redirect for search engines. This will help maintain your search engine rankings.

  3. Save the file as .htaccess in the root directory of the website that you are redirecting.

Now test to see if it works by visiting the website where you placed your .htaccess file. You should be instantly taken to your new domain with the corresponding page url.

Updates

9.19.2010.

  • Added line "RewriteRule ^ http://www.newdomain.com/ [R=301,L]" to the .htaccess code. This prevents a "Forbidden" error when accessing the root of a domain to be redirected and there is no index file present in the directory where the .htaccess file is located.
  • Added =301 to the [R] flag of the RewriteRule directive. Added comment that such indicates a 301 status code, or permanent redirect for search engines.