0

I am using one webserver for hosting two different websites, each having it's own domain and located in it's own subdirectory on the server. Both domains point to the root directory of my server.
This is my file structure:

root/
    domain1/
    domain2/

In my root directory i am using a small PHP script to determine which URL is coming in and than forward it to corresponding subdirectory.

if (($_SERVER['SERVER_NAME'] == "www.domain1.com" || $_SERVER['SERVER_NAME'] == "domain1.com") )  { 
   Header( "HTTP/1.1 301 Moved Permanently" ); 
   header("location: http://www.domain1.com/domain1"); 
}

else if (($_SERVER['SERVER_NAME'] == "www.domain2.com" ||  $_SERVER['SERVER_NAME'] == "domain2.com") )  {
   Header( "HTTP/1.1 301 Moved Permanently" );    
   header("location: http://www.domain2.com/domain2"); 
}

Up to here it works totally fine. When I call ww.domain1.com I get forwarded to the corresponding subdirecoty and the domain changes to www.domain1.com/domain1. This is where my question arrises: How can I hide the subdirectory in the URL? I have been struggeling with this for ages, reading guides to mod_rewrite and searching SO but didn't get anny success.
I have tested my server for RewriteEngine On, which works fine, I Just cant get the desired behaviour.

Edit:
Here is my htaccess code, located in the subdirectory domain1. The same code is located in directory2, changed to the naming conventions.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$
RewriteRule !^domain1/ domain1%{REQUEST_URI} [L]

I got the idea for this from SO: SO Article

Thanks in advance ;)

Community
  • 1
  • 1
turboLoop
  • 601
  • 1
  • 4
  • 16
  • _“Both domains point to the root directory of my server”_ – why? The _proper_ way to handle this would be to set up two VirtualHosts, that point them to the _correct_ respective directory to begin with. – CBroe Feb 13 '16 at 15:53
  • 1
    It is not my own server, it is just a web-space I rent, with a lot of unused resources. To save some costs, I decided to host both websites on the same webspace. – turboLoop Feb 13 '16 at 16:00

2 Answers2

0

you can use apache conditions in htaccess for this. create a file .htaccess with following code

 RewriteEngine on
 RewriteCond %{HTTP_HOST} ^(.*)\.domain1\.com
 RewriteRule ^(.*)$ /domain1/$1 [L,NC,QSA]

 RewriteCond %{HTTP_HOST} ^(.*)\.domain2\.com
 RewriteRule ^(.*)$ /domain2/$1 [L,NC,QSA]

this will pass all params too, in addition to silently redirecting to sub directory. your url in browser wont show the redirection.

Ranhot
  • 344
  • 2
  • 14
  • Thanks for your Answer, I am getting a "File not Found" error after implementing your suggestion. Any idea where this might come from? – turboLoop Feb 13 '16 at 15:58
0

You can use the following code in Root/.htaccess :

RewriteEngine on

#--Rewrite domain1 to /domain1 folder--#
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$
RewriteRule ^((?!domain1).*)$ /domain1/$1 [NC,L]
#--Rewrite domain2 to /domain2 folder--#
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.com$
RewriteRule ^((?!domain2).*)$ /domain2/$1 [NC,L]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115