whizzzkid
whizzzkid

Reputation: 1295

Masking sub domain with a new domain while preserving the paths

I own a domain since long, just masking the names:

http://mydomain.com

Later I started using a subdomain on this domain for some project.

http://subdomain.mydomain.com

Those projects grew and now I have a structure like

http://subdomain.mydomain.com/project1
http://subdomain.mydomain.com/project2
http://subdomain.mydomain.com/project3/subproject1
http://subdomain.mydomain.com/project3/subproject2
http://subdomain.mydomain.com/project3/subproject3
http://subdomain.mydomain.com/project4
....
etc.

now I bought a new domain (shortdomain.com) where I plan not to move anything but everything should be accessible via redirects so everything looks like:

http://shortdomain.com
http://shortdomain.com/project1
http://shortdomain.com/project2
http://shortdomain.com/project3/subproject1
http://shortdomain.com/project3/subproject2
http://shortdomain.com/project3/subproject3
http://shortdomain.com/project4
...
etc.

So basically I need to do two things: 1. if anyone visits my old domain, redirect them the new naming structure. i.e. if someone loads http://subdomain.mydomain.com/project2 they should be redirected to http://shortdomain.com/project2

  1. when a user loads/redirected to http://shortdomain.com/project2 this should actually load the content present at http://subdomain.mydomain.com/project2

So I will not manually migrate projects,codes and GBs of other data. I think this might be acievable by smart redirection only.

Just FYI: 1. I have full DNS control of both the domains 2. I am hosted on hostgator 3. I use cloudflare on the first domain and would like to continue using it

Upvotes: 3

Views: 4640

Answers (2)

anubhava
anubhava

Reputation: 785856

I have full DNS control of both the domains

With full control I assume you can enable mod_proxy as well on Apache web-server of shortdomain.com. Once that is done set it all up this way.

On subdomain.mydomain.com enable mod_rewrite and place this rule in Apache config OR DOCUMENT_ROOT/.htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule ^ http://shortdomain.com%{REQUEST_URI} [L,R=301]

On shortdomain.com enable mod_proxy, mod_rewrite and place this rule in Apache config OR DOCUMENT_ROOT/.htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^shortdomain\.com$ [NC]
RewriteRule ^ http://subdomain.mydomain.com%{REQUEST_URI} [L,P]

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143926

I think this might be acievable by smart redirection only.

No, redirection changes what's in the browser's location bar. If you redirect to shortdomain.com then the request will get sent to shortdomain.com, and have nothing to do with subdomain.mydomain.com anymore. If you redirect back to subdomain.mydomain.com, then the location bar in the browser will change as well.

What you really want to do is point shortdomain.com to the same server and document root that subdomain.mydomain.com is on. Then use this to redirect (either in htaccess file or server config):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://shortdomain.com/$1 [L,R=301]

If, for whatever absurd reason you can't point the shortdomain.com DNS to the same webserver that serves subdomain.mydomain.com, or can't setup that webserver to accept requests for the shortdomain.com host, you need to setup a proxy server. And it'll work something like this:

2 Webservers, server A (hosts subdomain.domain.com) and server B (hosts shortdomain.com)

  1. Someone requests http://subdomain.mydomain.com/project3/subproject1
  2. server A gets the request and redirects the browser to http://shortdomain.com/project3/subproject1
  3. browser's location bar changes to new location
  4. server B gets the request and reverse proxies the request back to server A
  5. server A gets the request again but must recognize that it is a proxy and then serve the page instead of redirecting

As you can see, this is a horrendously ineffecient solution. It's also a high possibility that your hosting service won't allow you to setup proxy servers.

Upvotes: 0

Related Questions