# Redirect to domain from subdomain

**URL:** <https://www.ruby-forum.com/t/redirect-to-domain-from-subdomain/174291>\
**Category:** Rails\
**Created:** [August 24, 2009, 3:37pm UTC](https://www.ruby-forum.com/t/redirect-to-domain-from-subdomain/174291 "2009-08-24T15:37:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_shukla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7cd45c/32.png) [@Abhishek\_shukla](https://www.ruby-forum.com/u/Abhishek_shukla)\
**Post date:** [August 24, 2009, 3:37pm UTC](https://www.ruby-forum.com/t/redirect-to-domain-from-subdomain/174291/1 "2009-08-24T15:37:14Z")

</div>

Hello Friends,  
Just wanted to know how can i execute the following task

My requirement is

1. Let say the main domain is [www.maindomain.com](http://www.maindomain.com)
2. And a subdomain is e.g. [xyz.maindomain.com](http://xyz.maindomain.com) and Every subdomain have a  
correspondence domain store in DB. so that user can access a site using  
that  
domain as well.  
For e.g let say there is a subdomain [xyz.maindomain.com](http://xyz.maindomain.com) and this can be  
access using [www.xyz.com](http://www.xyz.com)

So just wanted to know its need to be configured in .htaccess file or  
i’ll  
need to configure something else?

NOTE: the subdomain will be dynamic.

Thanks  
Abhis

---

<div class="post-metadata">

**Author:** ![Abhishek\_shukla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7cd45c/32.png) [@Abhishek\_shukla](https://www.ruby-forum.com/u/Abhishek_shukla)\
**Post date:** [August 26, 2009, 9:09am UTC](https://www.ruby-forum.com/t/redirect-to-domain-from-subdomain/174291/2 "2009-08-26T09:09:56Z")

</div>

ok let me be more specific

I have two domain which in pointing to my rails application and a  
subdomain

1. [www.xyz.com](http://www.xyz.com)
2. www.xyz.info
3. [example.xyz.com](http://example.xyz.com)

Now when the user access www.xyz.info the [example.xyz.com](http://example.xyz.com) page should  
open up but the address bar should be www.xyz.info.

So is it possible to redirect?

Thanks Abhi

---

<div class="post-metadata">

**Author:** ![Abhishek\_shukla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7cd45c/32.png) [@Abhishek\_shukla](https://www.ruby-forum.com/u/Abhishek_shukla)\
**Post date:** [August 25, 2009, 5:20pm UTC](https://www.ruby-forum.com/t/redirect-to-domain-from-subdomain/174291/3 "2009-08-25T17:20:31Z")

</div>

If you have a domain maindomain then you got to do whatever you need  
within that domain.  
How can you use other’s domain?you can use subdomain on your  
maindomain but xyz is some other domain.  
You can’t use that to redirect to your domain by anyway.

---

<div class="post-metadata">

**Author:** ![Abhishek\_shukla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7cd45c/32.png) [@Abhishek\_shukla](https://www.ruby-forum.com/u/Abhishek_shukla)\
**Post date:** [August 26, 2009, 10:25am UTC](https://www.ruby-forum.com/t/redirect-to-domain-from-subdomain/174291/4 "2009-08-26T10:25:24Z")

</div>

On Tue, Aug 25, 2009 at 11:58 PM, Abhishek shukla[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have two domain which in pointing to my rails application and a subdomain
> 
> 1. [www.xyz.com](http://www.xyz.com)
> 2. www.xyz.info
> 3. [example.xyz.com](http://example.xyz.com)

Actually, no. You have three host names. “[xyz.com](http://xyz.com)” and “xyz.info”  
are domain names. “[example.xyz.com](http://example.xyz.com)” /could/ be a subdomain, if  
you have hosts within that subdomain like ‘[ren.example.xyz.com](http://ren.example.xyz.com)’,  
‘[stimpy.example.xyz.com](http://stimpy.example.xyz.com)’-- but I’m betting that’s not the case.

> Now when the user access www.xyz.info the [example.xyz.com](http://example.xyz.com) page should  
> open up but the address bar should be www.xyz.info.
> 
> So is it possible to redirect?

Why don’t you just alias (in DNS) one to the other?

And no, it’s not possible to “redirect” and have the url in the address  
bar remain the same.

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Abhishek\_shukla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7cd45c/32.png) [@Abhishek\_shukla](https://www.ruby-forum.com/u/Abhishek_shukla)\
**Post date:** [August 26, 2009, 7:18pm UTC](https://www.ruby-forum.com/t/redirect-to-domain-from-subdomain/174291/5 "2009-08-26T19:18:50Z")

</div>

On Wed, Aug 26, 2009 at 3:24 AM, Hassan  
Schroeder[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And no, it’s not possible to “redirect” and have the url in the address  
> bar remain the same.

This is exactly what Apache’s mod\_rewrite does. You send it a url and  
it rewrites the request to the actual resource.

RewriteEngine On  
RewriteBase /  
RewriteCond %{HTTP\_HOST} ^old.com$  
RewriteRule ^(.\*) httq://new.com/$1 [L,R=301]

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![Abhishek\_shukla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7cd45c/32.png) [@Abhishek\_shukla](https://www.ruby-forum.com/u/Abhishek_shukla)\
**Post date:** [August 26, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/redirect-to-domain-from-subdomain/174291/6 "2009-08-26T20:04:30Z")

</div>

On Wed, Aug 26, 2009 at 8:37 AM, Greg D.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > And no, it’s not possible to “redirect” and have the url in the address  
> > bar remain the same.
> 
> This is exactly what Apache’s mod\_rewrite does. You send it a url and  
> it rewrites the request to the actual resource.

If a server sends a 30x redirect response to a client, it instructs that  
client to send a new request to the provided URL, which will then be  
visible in the client’s address bar (if it has one).

Forwarding “under the covers” is a different beast.

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Abhishek\_shukla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7cd45c/32.png) [@Abhishek\_shukla](https://www.ruby-forum.com/u/Abhishek_shukla)\
**Post date:** [August 27, 2009, 11:38pm UTC](https://www.ruby-forum.com/t/redirect-to-domain-from-subdomain/174291/7 "2009-08-27T23:38:13Z")

</div>

This is mostly about Apache (or equivalent) settings higher up  
than .htaccess - look around for documents regarding “name-based  
virtual hosting”. It’s quite possible to set up - pretty much every  
shared webhost out there is doing it.

–Matt J.
