How to Completely Redirect Whole Request (With Path and Query String) From One Domain to Another?

2018-02-28 htaccess

Let’s assume your full name is Billy Example and you’re lucky enough to be owner of the following domains:

  • billyexample.com
  • billexample.com
  • bexample.com
  • example.com (why not)

You want to use example.com as the main domain and point all other domains to the main one. By saying “point to” I mean redirect absolutely every GET request with any combination of paths and query strings.

For example:

What user requested Where user will land
http://billyexample.com http://example.com
http://billexample.com/about/ http://example.com/about/
http://bexample.com/foo/bar?baz=1&zaz=whynot http://example.com/foo/bar?baz=1&zaz=whynot

All requests from each domain you own will be piped to the main one without loosing the request context. To achieve this all you need to do is to create .htaccess file and put it into public_html (or similar directory) on your hosting server for each of the domain that should redirect.

RewriteEngine on
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]