Redirect primary domain to sub folder without affecting secondary domain using .htaccess

I have 2 domains:

  • gyanplease.com/gyanplease primary domain (I have changed the document root with the help of .htaccess)
  • mobiledevsolutions.com secondary domain

On the document root I have created a folder called gyanplease and with the help of .htaccess I have rewritten gyanplease.com to the gyanplease the folder. This the code for the redirection:

#disable https
RewriteEngine on
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !gyanplease/
RewriteRule (.*) /gyanplease/$1 [L]

Now when I'm hosting another domain on the same server, that is mobiledevsolutions.com, then it's not working and gives a 404 redirection error.

1 answer

  • answered 2017-06-17 19:54 SuperDuperApps

    What you can do is only run the /gyanplease/ rewrite for that host, like this (replacing the last two lines):

    RewriteCond %{HTTP_HOST} (?:^|\.)gyanplease.com$
    RewriteCond %{REQUEST_URI} !^/gyanplease/
    RewriteRule ^(.*)$ /gyanplease/$1 [L]
    

    That way this rule will only affect gyanplease.com.

    You can also change line 4 to this, since the capturing is not being used:

    RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    So all that would make your new rules:

    RewriteEngine on
    # Disable HTTPS
    RewriteCond %{HTTPS} on
    RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Rewrite gyanplease.com to /gyanplease/
    RewriteCond %{HTTP_HOST} (?:^|\.)gyanplease.com$
    RewriteCond %{REQUEST_URI} !^/gyanplease/
    RewriteRule ^(.*)$ /gyanplease/$1 [L]