17

How can I redirect all requests (irrespective of what page is being requested) on sub.domain.com to newdomain.com? Currently I have

Redirect 301 / http://www.newdomain.com/

When a requests comes in for domain.com/shop/product the redirect goes to newdomain.com/shop/product while it should just go to newdomain.com

T.Todua
  • 53,146
  • 19
  • 236
  • 237
stef
  • 26,771
  • 31
  • 105
  • 143

2 Answers2

32

Use Rewrite:

RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]
Sven Koschnicke
  • 6,523
  • 2
  • 34
  • 49
  • 2
    This takes every request to `http://www.newdomain.com` but does not forward them. – Starx Dec 13 '12 at 23:20
  • Why do you think that? The docs state it quite clearly: "Use of the [R] flag causes a HTTP redirect to be issued to the browser. If a fully-qualified URL is specified (that is, including `http://servername/`) then a redirect will be issued to that location. Otherwise, the current protocol, servername, and port number will be used to generate the URL sent with the redirect." http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_r – Sven Koschnicke Dec 14 '12 at 08:09
  • First of all I tested it and didn't work, then I went on to find out the cause of it because as you said this is a valid rule, but having `/$1` at the end worked on most of my servers (I haven't tested in all). – Starx Dec 14 '12 at 13:40
  • 4
    Adding `/$1` will add the path of the request to the new domain. That is not what the OP intendet. For seeing what is going on, you can increase the `RewriteLogLevel`: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteloglevel – Sven Koschnicke Dec 14 '12 at 14:33
8
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

That will forward any GET requests.

Grzegorz Rożniecki
  • 27,415
  • 11
  • 90
  • 112
AzureMichael
  • 81
  • 1
  • 1