43

What I want to do is the following:

My domain xy.example.com no longer exists. Thus I want to do a simple redirect to the new domain abc.example.com. It should be a redirect, that also works when someone types in the browser bar http://xy.example.com/team.php - than it shoul redirect to http://abc.example.com/team.php

I've already tried a few things, but it didn't really work. What do I have to put in the Apache 2 config?

JohnnyFromBF
  • 1,289

3 Answers3

70

You can use the RedirectPermanent directive to redirect the client to your new URL.

Just create a very simple VirtualHost for the old domain in which you redirect it to the new domain:

<VirtualHost *:80>
    ServerName xy.example.com
    RedirectPermanent / http://abc.example.com/
    # optionally add an AccessLog directive for
    # logging the requests and do some statistics
</VirtualHost>
joschi
  • 21,955
18

Create or edit a .htaccess inside your DocumentRoot. Add

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

Additionally I would change the ServerName directive to the new domain and leave a ServerAlias with the old domain.

ServerName abc.example.com
ServerAlias xy.example.com
Chris
  • 1,215
4

VH is OK if you can do it, but not really a drop-in solution.

I prefer using If:

<If "%{HTTP_HOST} == 'old.example.com'">
    Redirect "/" "https://new.example.com/"
</If>

This is something you could use in the same place you define ServerAlias. And should work fine in multi-tenant environment.

Nux
  • 703