barush
barush

Reputation: 333

Apache redirect for multiple domains

I have a series of domains like:

payroll.olddomain.com
billing.olddomain.com
support.olddomain.com
etc...

I need to redirect all of them to:

payroll.newdomain.com
billing.newdomain.com
support.newdomain.com
...

I could approach this in brute force fashion with a series of virtual host paragraphs, one paragraph per domain, as in:

<VirtualHost *:80>
    ServerName payroll.olddomain.com
    Redirect permanent / http://payroll.newdomain.com/
</VirtualHost>
...

Or I could use a series of rewrites, one per domain, as in:

RewriteCond "%{HTTP_HOST}"   "payroll.olddomain.com" [NC]
RewriteRule .  "payroll.newdomain.com%{REQUEST_URI}" [L,R=301]
...

But what I would really like to do, if possible, is find a simple and elegant way to (pseudo-code):

redirect <anything>.olddomain.com to <anything>.newdomain.com

I would assume a solution would include a regexp thingy, something like (more pseudo):

redirect (.*)\.olddomain\.com to http://$1.newdomain.com

Any ideas on how to accomplish this? Is it even possible?

Thanks!

Upvotes: 1

Views: 92

Answers (1)

Example person
Example person

Reputation: 3346

This should work. Place this in your root .htaccess file. Make sure mod_rewrite is enabled.

RewriteEngine On #Don't use twice.

RewriteCond %{HTTP_HOST} ^(.+?)\.olddomain\.com$ [NC]
RewriteRule ^ http://%1.newdomain.com%{REQUEST_URI} [R=301,L]

Upvotes: 1

Related Questions