Aman Maurya
Aman Maurya

Reputation: 1325

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

I have 2 domains:

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.

Upvotes: 2

Views: 79

Answers (1)

user2493235
user2493235

Reputation:

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]

Upvotes: 1

Related Questions