Htaccess – mask and redirect domain to sub directory on shared hosting

.htaccessmaskredirectshared-hosting

Hopefully someone can help…Server nube setting up new hosting acct

Problem definition:

  • New shared hosting account, want to set up multiple domains / sites on it
  • You have to specify a 'main domain', then set up sub directories to point the other domains at

Required solotion:

  • I don't really want to have all my other sites located in the root of my primary site so want to have all my sites including the primary one in separate sub directories.

So 'www.maindomain.co.uk' actually points to www.maindomain.co.uk/sub_dir1/ BUT shows up in the users address bar as 'www.maindomain.co.uk'

Also I would like for all requests to 'www.maindomain.co.uk/sub_dir1/' to just show up as 'www.maindomain.co.uk'

Can anyone advise how to do this – via htaccess maybe?

Also I have had issues with google showing 'myotherwebsite.com' as 'www.maindomain.co.uk/myotherwebsite' is there a way to prevent this.

If I am missing something obvious please forgive me as am a server nube and can not find any answers re this on the site.

Thanks in advance.

Steve

Best Answer

So, basically, you want to ensure that when a page is requested it is not being accessed from a maindomain.co.uk/<subfolder> path, right?

Drop an .htaccess file like the one below in each directory which hosts its own site:

<IfModule mod_rewrite.c>

RewriteEngine On

# force SEPARATE_DOMAIN with www-prefix
RewriteCond %{HTTP_HOST} !^www\.SEPARATE_DOMAIN\.co\.uk$
RewriteRule .* http://www.SEPARATE_DOMAIN.co.uk$1 [R=301,QSA]

</IfModule>

Replace SEPARATE_DOMAIN with the name of the domain pointed to the directory you're working with and any requests to that directory from the main domain will be permanently redirected to the domain you specify.

Ex: maindomain.co.uk/myotherdomain/index.php?query=string will redirect to myotherdomain.co.uk/index.php?query=string

Related Topic