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?


You could always use a simple VirtualHost:

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

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]
  • I tried both and they didn't work. I have mod_rewrite enabled and I have the VirtualHost in a separate site file. Is there anything that I'm missing? – Technius May 21 '13 at 5:16
  • 3
    This works fine. I had a loop redirect problem, because I was pointing a subdomain to a subfolder, and that subfolder was redirecting. Now, I redirect the subdomain to the URL that corresponds to the folder, and the 2nd redirection happens just fine! – Paschalis Jul 12 '14 at 9:29
  • How to do ot to preserve http:// or https://, whatever way b.website.com was accessed in the first place? – Golar Ramblar Jun 2 '18 at 8:27

Complementing the main answer

Redirect type

You can explicitly specify the type of redirect you pretend.
I suggest you use a temporary redirect (302) while testing the redirection rule.

# In a VirtualHost file
Redirect [301|302] /old_location http://new_domain/newlocation

# In a .httaccess file
RewriteRule ^(.*)$ http://new_domain/$1 [R=302,L]

Specify directory matching patterns

You could only redirect requests that match some pattern.

# In a VirtualHost file
RedirectMatch [301|302] ^/public/(.*)$ http://public.example.com/$1

# In a .httaccess file
RewriteRule ^/public/(.*)$ http://public.example.com/$1 [R=302,L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.