- 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.
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.
- 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.
- Find (Ctrl+F) “mod_rewrite”. Uncomment the following line:
#LoadModule rewrite_module modules/mod_rewrite.soTo uncomment, remove the # sign that preceedes it. This enables the rewrite module mod_rewrite during Apache server startup or restart.
- Find (Ctrl+F) “.htaccess”. Change the word “None” in the following line to All:
AllowOverride NoneThis allows files with filename .htaccess to control the rewrite module with directives for the directory and/or subdirectories that contains it.
- Save the configuration file.
- 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.
- Open Notepad or your preferred plain text editor. In Windows, the easiest way to do this is by clicking Start > Run... > type notepad > OK
- 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.
- 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.
- 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.