Apache2 - Redirecting a subdomain to another URL

I have two subdomains, a.website.com and b.website.com, pointing to the same IP address. I want to redirect b.website.com to a.website.com:8080. I have this in my .htaccess file...

RewriteEngine on
RewriteCond {HTTP_HOST} b\.website\.com
RewriteRule ^(.*)$ http://b.website.com:8080$1 [L]

...but it does not work.

Is there a way to make it work?


Solution 1:

You could always use a simple VirtualHost:

<VirtualHost *:80>
  ServerName b.website.com
  RedirectPermanent / http://a.website.com:8080/
</VirtualHost>

If you prefer to go with the .htaccess file, you're just missing a % sign on the Rewrite Condition:

RewriteEngine on
RewriteCond %{HTTP_HOST} b.website.com
RewriteRule ^(.*)$ http://a.website.com:8080$1 [L]