Skip to content
Advertisement

How to redirect subdomain to main domain maintaining the same URL?

My main domain(www.mysite.com) is pointing to a folder in my server (/home/user_name/htdocs/mysite.com).

I have recently created a subdomain(m.mysite.com) through my hosting account. But it is pointing it to a new folder (/home/user_name/htdocs/m.mysite.com) in my server. I am not able to edit the path to which this new subdomain points to. All my files are hosted in my main folder path.

My Requirement –

When a user visits m.mysite.com he should be redirected to mysite.com but the URL in the browser should still say m.mysite.com
I’m handling the mobile view by detecting the subdomain using $_SERVER[‘SERVER_NAME’]

Advertisement

Answer

I think you should able to write an .htaccess file with this rule, provided it is the same user and has read permisssions to both URLs:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^m.mysite.com
RewriteRule (.*) ../mysite.com/$1 [L]

Or, you can make a symlink to the main site, let’s say:

./mysite.com
    dirs and folders

./m.mysite.com
    drwxr-xr-x main_site -> ../mysite.com

And an htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/?mysite.com 404.html [L] # Do not show the real filesystem layer
RewriteRule (.*) ./mysite.com/$1 [L]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement