Trick to redirect website root page

I needed to do two things –

  1. redirect all access to my website without www prefix(example example.com) to url with prefix www (www.example.com).
  2. redirect root page of the website to a page under some subdirectory. That is example.com should redirect to www.example.com/pages/about/about.html

Website is hosted on bluehost. I later learnt the website is using apache server. It is simple stuff for someone who is hosting or building websites for long. I need to search and try couple of tricks on the cpanel before I could get it to work.

Here is how it works:

  1. There is .htaccess file in the public_html (root folder). File is hidden and file manager cpanel needs to be launched with checkbox (hidden) ticked.
  2. Edit this file.
  3. This file is processed by apache web server, and url rewrite rules can be written in this file.
  4. script snippet below saved in the .htaccess file, worked for me to achieve this.
  5. you can refer the documentation of redirects here.
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^$ http://www.example.com/pages/about/about.html [L,R=301]
Advertisements
This entry was posted in Hosting Website and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s