20

We need to run a few subdomains from our main site and we need to point these to external sites that deal with the admin required.

I simply need to redirect a subdomain to an external URL using .htaccess and also any advise about where to put it in the .htaccess file e.g. right at the top, as i know this effects certain rewrite rules.

I won't write what i think it should be as this just leads the answer down a specific route.

Cheers Guys,

Really appreciate it.

Dan

Dan
  • 377
  • 1
  • 4
  • 12

3 Answers3

36

If you are redirecting a subdomain to another domain then there shouldn't be anything else in the .htaccess file other than this.

 # This will redirect a subdomain to another domain
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^yoursubdomain\.yourdomain\.com$ [NC]
 RewriteRule ^(.*) http://www.newdomain.com/$1 [L,R]
Simon Hayter
  • 3,131
  • 27
  • 53
  • This did not work, I have tried this exact method previously but for some reason it wont seem to take. I am not sure why?? Any other ideas? – Dan Dec 27 '12 at 09:34
  • This works great, but I was wondering...do you know a way to do this without changing the URL? So the page redirects but still keeps `yoursubdomain.yourdomain.com` in the address bar. – jlewkovich Jun 09 '15 at 03:22
  • @JLewkovich there is no way to do that. The idea of the address bar to is to tell people where they are. However You could iframe the other site. – Simon Hayter Jun 09 '15 at 08:57
  • Line 5-6 doesn't works. Are the first definitions affecting that. https://gist.github.com/santosh/134f7f3c36a6dd7f11f1 – Santosh Kumar Jun 18 '15 at 20:14
  • @JLewkovich, this is done in the answer provided [here](https://stackoverflow.com/questions/23027847/htaccess-specific-url-to-different-port). You'll simply have to tweak it a bit for the subdomain. – Chris - Jr Jul 24 '17 at 16:45
1

Here's the code I used and it worked for me:

# BEGIN CUSTOM Redirects
# This will redirect a subdomain to another domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^pw\.example\.com$ [NC]
RewriteRule ^(.*) https://www.example.com [L,R
Simon Hayter
  • 3,131
  • 27
  • 53
King R
  • 23
  • 1
  • 1
  • 5
1

I tried both options but didn't work until I remove the $ and [L,R] this is the code that worked for me

 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
 RewriteRule ^(.*) http://newdomain.com.ar/?param=data&uparam2=data

hope it helps.

Francisco
  • 11
  • 1