I have a website like www.example.com, I want to redirect each hit to this URL and Sub URL to a single URL on subdomain. Following are some examples

www.example.com should be redirected to http://test.example.com

www.example.com/show/mypage1 should be redirected to http://test.example.com

www.example.com/show/mypage2 should be redirected to http://test.example.com

www.example.com/show/mypage3 should be redirected to http://test.example.com

I want to do it using .htaccess. and I want 302 temporary redirect. I am using Apache WebServer.

Accepted Answer

Are you using Apache or IIS?

In Apache:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://test.example.com/ [L,R=302]

edited

edited: replaced http://test.example.com/$1 to http://test.example.com/, it works now as required.

Written by blomman9
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki