6

I'm looking to redirect each and every page on the old domain to a single page on the new domain. It should also redirect both the www and non-www versions. Every single request to the domain old.com, whatever it may be, should lead to www.new.com root.

old.com/1.php
www.old.com/2.php
old.com/2.php
old.com/links/index.php
old.com/about-us.html
old.com/?string=1

should all redirect to:

www.new.com/

I'm looking for a .htaccess solution.

Dan Horvat
  • 800
  • 3
  • 14
  • 27

3 Answers3

19

You can use RedirectMatch in the old.com vhost

RedirectMatch permanent .* http://www.new.com/

Redirect appends the trailing portion of the matched URI to the redirection URI but with RedirectMatch it's up to you to choose the parts (if any) that you want to transfer using parentheses and $number references.

If the old and new domains must be aliases for the same document root on disk then you'll probably have to use mod_rewrite

RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/ [L,R=301]
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Works for most pages but not for www.old.com/?string=1 – Dan Horvat Feb 24 '13 at 22:11
  • The second solution also doesn't redirect www.old.com/?string=1 (which is the equivalent of www.old.com/index.php?string=1). It does redirect all URLs which are in form of folders though (www.old.com/folder/) – Dan Horvat Feb 24 '13 at 22:28
  • 3
    @DanHorvat If you append a question mark `?` to the new domain, the query string will be removed `RewriteRule .* http://www.new.com/? [L,R=301]` – Olaf Dietsche Feb 24 '13 at 22:52
  • In order to keep all query strings, please add `[QSA]` at the end of the line containing the RewriteRule – Marco Marsala Mar 04 '16 at 11:40
  • This is the working solution. Redirect ALL pages to a single page on a new domain. – metal_jacke1 Oct 07 '22 at 15:45
4

This has already been answered, but here's the code altogether containing helpful comments so people have a reference to look back on later should they forget what it does.

Put this in your .htaccess file:

## Each and every page on old domain redirects to single page
## By appending question mark to new domain, query strings are removed
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/? [L,R=301]

You can also redirect to a sub directory on another domain, as was the case that I needed to do like this:

## Each and every page on old domain redirects to single page
## By appending question mark to new domain, query strings are removed
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/sub-directory/? [L,R=301]
Ian B
  • 41
  • 1
0

This will handle all your redirects including query strings. Excuse the formatting, I'm on a phone :)

RewriteCond %{HTTP_HOST} old\.com$ RewriteCond %{QUERY_STRING} .+
RewriteRule (.*) http://www.new.com$1?%{QUERY_STRING} [L,R=301,QSA]

RewriteCond %{HTTP_HOST} old\.com$
RewriteRule (.*) http://www.new.com$1 [L,R=301,QSA]
kjetilh
  • 4,821
  • 2
  • 18
  • 24