htaccess 301 redirect domain 1 > domain 2

Hi All -

I'm running Ubuntu 12.04 and Apache. I have 2 domains setup under VPS on the same linode. I would like domain 1 to redirect to domain 2 using a 301 redirect (so I would rather not use a CNAME). Additionally, I want to make sure both the top-level host and www host redirect to domain 2.

Both domains have been setup on the linode with a VPS. DNS is using linode, and I've setup A records for each pointing to the ip address of my linode. Both domains are currently resolving though domain 1 shows the content for domain 2 (without redirecting). I've verified that mod_rewrite is running.

However, my htaccess file is not having any effect in terms of getting the 301 redirect to work. The htaccess file in the public folder under domain 1.

This is what is in my htaccess file:

htaccess for domain1.com rewriting to all requests to domains2.com

#htaccess for domain1.com rewriting to all requests to domains2.com 

RewriteEngine On
Rewritecond %{HTTP_HOST} !^www\.domain2\.com
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]

Thanks for your help!

3 Replies

If you want domain1 to be redirected to domain2 shouldn't you have:

# sending domain1 to domain2
Rewritecond %{HTTP_HOST} !^domain1\.com$ [OR]
Rewritecond %{HTTP_HOST} !^www\.domain1\.com$
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]

This assumes you have this in the .htaccess file in the root directory of domain1.com

MSJ

A couple things…

Why are you trying to do this in a .htaccess file and not in the virtual host configuration?

What is the AllowOverride directive set to in the the apache2.conf file / virtual host config for domain1.com?

Your .htaccess file includes the dot (.) correct? All mentions in your post above do not have it.

You can also do this with mod_alias:

Redirect permanent / http://domain.tld/

Redirects all traffic in that VirtualHost to that URL and adds any URL params and such. You can also do this right inside of the VirtualHost block in your Apache config.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct