htaccess 301 redirect domain 1 > domain 2
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
# 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
Why are you trying to do this in a .htaccess file and not in the virtual host configuration?
What is the AllowOverride directive
Your .htaccess file includes the dot (.) correct? All mentions in your post above do not have it.
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.