?
Solved

Ho to redirect a domain with htaccess?

Posted on 2011-10-31
4
Medium Priority
?
457 Views
Last Modified: 2012-05-12
Hello,

i have 2 Domains.

www.domain1.com
www.domain2.com

1.) Every Domain haves the same webspace. If somebody type in www.domain2.com he should be redirected to www.domain1.com

2.) If somebody type in domain1.com or domain2.com (without the www.), he also should be redirected to www.domain1.com

Please can somebody explain how to solve this with htaccess on a Linux Server?

Thank´s!
0
Comment
Question by:druplash
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Hellmark
ID: 37055309
for the .htaccess in domain1
#Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Open in new window


for the .htaccess on domain2
#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domain2.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain2.com$
RewriteRule ^(.*)$ http://www.domain1.com/ [R=301,L] 

Open in new window

0
 

Author Comment

by:druplash
ID: 37055326
Thank´s for your reply.
But i have only 1 webspace and 1 htaccess file. Should i write all these rules in 1 htaccess file?
0
 
LVL 7

Accepted Solution

by:
Hellmark earned 2000 total points
ID: 37055466
Are both domains pointed at the same server? Typically, many hosts will try to have a separate directory for each domain. If they're both sharing the same directory (ie, same contents appear when you load up either domain), and you just want to force the use of one domain, then you would do something like this
#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1.com [OR]
RewriteCond %{HTTP_HOST} ^www.domain2.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain2.com$
RewriteRule ^(.*)$ http://www.domain1.com/ [R=301,L] 

Open in new window


If you do not have your domain associated with your server, then you cannot use .htaccess redirects.
0
 

Author Closing Comment

by:druplash
ID: 37055510
Yes, both are sharing the same directory. And your solution works great!
Many thank´s!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question