Ad Marketplace and Auctions
 
Home Register FAQ Members List Calendar Search Today's Posts Mark Forums Read Web Directory

Go Back   Webmaster Forum > Designing and Developing Websites > Web & Server Administration

Web & Server Administration Discussions about websites and server administration, mod_rewrite, managing .htaccess, Apache and Windows Server configuration, LINUX, Security, Traffic Analysis, Sitemaps, robots.txt etc...

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2008, 10:14 PM
tonyf12 tonyf12 is offline
WMG Newcomer

 
Join Date: Jul 2008
Posts: 32
iTrader: (0)
tonyf12 is on a distinguished road
Default HOWTO: Redirect all your domains to one domain using .htaccess

Note: The forum will not allow me to post links until my post count is higher so in this post replace h++p with http and tonyswebdesign with your own domain(s)

If you have more than one domain for your website, it could hurt your search engine rankings as most search engines can't tell if the domains are the same web page. While you can redirect using the
HTML Code:
<meta http-equiv="refresh" content="5;url=h++p;//example.com/" />
tag to redirect, a better way is to use Apache's .htaccess.

On my website I have 4 domains each working with or without the www . so that is 8 ways to access the page. That would mean each page would have to compete in the search engine with itself 7 times (or for the smarter search engines 3 times). My .htaccess file is shown below:

Code:
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.tonyswebdesign\.com$ [NC]
RewriteRule ^(.*)$ h++p://tonyswebdesign.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.tonyswebdesign\.org$ [NC]
RewriteRule ^(.*)$ h++p://tonyswebdesign.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.tonyswebdesign\.net$ [NC]
RewriteRule ^(.*)$ h++p://tonyswebdesign.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.tonyswebdesign\.co\.uk$ [NC]
RewriteRule ^(.*)$ h++p://tonyswebdesign.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^tonyswebdesign\.net$ [NC]
RewriteRule ^(.*)$ h++p://tonyswebdesign.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^tonyswebdesign\.org$ [NC]
RewriteRule ^(.*)$ h++p://tonyswebdesign.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^tonyswebdesign\.co\.uk$ [NC]
RewriteRule ^(.*)$ h++p://tonyswebdesign.com/$1 [L,R=301]
If you want to just copy and paste the code and replace my domain with yours, remember to put a \ before any dots.

How it works
The first line in the file
Code:
RewriteEngine on
activates apache's mod_rewrite program for redirecting urls.

The first rule of each pair is the condition for testing.
Code:
RewriteCond %{HTTP_HOST} ^www\.tonyswebdesign\.co\.uk$ [NC]
In this example if the HTTP_HOST (the domain) matches the regex
Code:
^www\.tonyswebdesign\.co\.uk$
meaning that it is
Code:
www . tonyswebdesign.co.uk
without anything before (because it starts with ^) or anything after (because it ends with $), the url will be changed.

The slashes in the regex are there beacuse dots have a special meaning and the slash tells apache that the dot is a dot and not a rule.

The [NC] part tells it not to check the case of the host.

The second part of each pair is the actual url rewrite.

Code:
RewriteRule ^(.*)$ h++p://tonyswebdesign.com/$1 [L,R=301]
It is in three parts. The first is RewriteRule, then the url to be changed, then the url to change it to.

In this example it changes any number of anything
Code:
^(.*)$
to
Code:
h++p://tonyswebdesign.com/$1
where the $ means an earlier part of the rule in brackets (the first one in this case, indicated by the $1).

The L can be ignored for now and the R=301 means it is a 301 redirect (which means permantly moved) so next time the browser/search engine will know to check the new url instead.

Just repeat for each domain you need to rewrite.

Note: not all web hosts allow use of .htaccess files and this will only work on apache (not IIS).
Reply With Quote
Sponsored Links
Register and sign in to hide this ad block

Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT. The time now is 06:49 PM.



Freelance Web Designers
Work At Home Forum
Ad Marketplace
Online Deals and Bargains
Lunarpages Web Hosting Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0