39

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,239
  • 6
  • 21
  • 25
  • 4
    A late comment. if xy.example.com does not *exist* anymore meaning there is no ip-adress for xy.example.com, nobody will go anywhere when they type that in the browser. The domain must exist before anyone can go there to be redirected. It's like putting a physical answering machine on your old telephone line giving your new number, then disconnecting the line. – Lenne Jan 07 '16 at 09:37
  • For ISPConfig redirect, see https://www.howtoforge.com/community/threads/redirecting-a-domain-to-external-website.56389/ – Fernando Kosh Mar 22 '19 at 03:12

3 Answers3

68

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
  • 20,747
  • 3
  • 46
  • 50
  • Actually not so simple because you might need to redir both http and https and that becomes two VH. And also you need to add ssl config... So probably easier to use mod_rewrite. – Nux Apr 01 '21 at 13:11
  • @Nux That wasn't asked, so it wasn't answered. – joschi Apr 02 '21 at 20:45
15

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,155
  • 2
  • 9
  • 18
  • 1
    I cant see how this would work on its own. A RedirectCond !^xy.example.com$ is required to prevent a forwarding loop. – GeoSword Nov 20 '18 at 12:00
  • 1
    The ServerAlias solution would not work with HTTPS, if a certificate for `abc.example.com` is used. Upon connecting to `https://xy.example.com` the browser would display a certificate error and ask user confirmation to continue. Also it is not a "redirection" to `abc.example.com`, because the browser location URL would not be changed, but kept as `xy.example.com`. – Snackoverflow Dec 13 '19 at 13:46
  • $1 by default contains the index path /. So we should remove /. http://abc.example.com$1 – Chen Hanhan Feb 26 '22 at 21:41
1

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
  • 541
  • 3
  • 12
  • 21
  • This is neat! Does it work in the .htaccess too? – Orphans May 31 '21 at 11:04
  • Hm... Haven't tried, but both directives are allowed in hta, so should work. See: https://httpd.apache.org/docs/2.4/mod/core.html#if https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect – Nux Jun 01 '21 at 21:31
  • Do note that using hta makes requests a bit slower. Not much, but generally config is conidered faster. – Nux Jun 01 '21 at 21:34
  • If this works with .htaccess - it is a nice solution for a multisite in a shared host environment. Thanks – Orphans Jun 02 '21 at 13:33