Multiple domains to dir with htaccess

Hello,

I am trying to setup my site so that I can have multiple domains pointing to specific dirs on the server but I am having some trouble.

Here is what my htaccess starting point looks like:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\\.com [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I add the additional domains by adding:

RewriteCond %{HTTP_HOST} ^domain.kr$ [OR]
RewriteCond %{HTTP_HOST} ^domain.kr$
RewriteRule ^/?$ "http\\:\\/\\/domain\\.com\\/kr\\/"

When I add other domains to it they all end up pointing to the root. What am I doing wrong?

Thanks

I’ll get to your answer later, but first please allow me to walk you through some lines.


RewriteBase /

Are you using Redirect, RedirectMatch, or any mod_alias directives? If not, ditch that line; you don’t need it.


RewriteCond %{HTTP_HOST} .

What do you expect this line to do? Hint: currently it does nothing at all


RewriteCond %{HTTP_HOST} !^domain\\.com [NC]
RewriteRule (.*) http://domain.com/$1 [R=301,L]

A more elegant way of writing that is


RewriteCond %{HTTP_HOST} !^domain\\.com [NC]
RewriteRule .? http://domain.com%{REQUEST_URI} [R=301,L]

As the (.*) is creating an atom, which you don’t really need because the value is already known in %{REQUEST_URI} so you might as well use that.


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

that could be better written as


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? index.php?q=%{REQUEST_URI} [L,QSA]

For the same reason as above.
On apache 2.x this will introduce an extra / at the start of ?q=
If you don’t want that either trim the first character from the q variable in your index.php, or use the rule you previously had (with the (.*) )

RewriteCond %{HTTP_HOST} ^domain.kr$ [OR]
RewriteCond %{HTTP_HOST} ^domain.kr$
RewriteRule ^/?$ "http\\:\\/\\/domain\\.com\\/kr\\/"

If the domain is domain.kr or the domain is domain.kr -> that’s the same thing, you can loose one of them.
Also, checking if no path was requested is better solved with %{REQUEST_URI} and lastly, why are you using double quotes, escaping slashes (and the semi-column!?) and not putting a redirect and last flag on that last line, and not escaping the dot in domain.kr and not using the NoCase flag on the RewriteCond!? You did that all correct before (with the rewrite to domain.com), so why mess it up here?


RewriteCond %{HTTP_HOST} ^domain\\.kr$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .? http://domain.com/kr [L,R=301]

Now, as for the problem you’re having. The thing is that Apache will first evaluate


RewriteCond %{HTTP_HOST} !^domain\\.com [NC]
RewriteRule .? http://domain.com%{REQUEST_URI} [R=301,L]

So, if the domain is not domain.com (and domain.kr is not domain.com) it will rewrite to domain.com. So then


RewriteCond %{HTTP_HOST} ^domain\\.kr$
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .? http://domain.com/kr [L,R=301]

won’t do anything anymore, since domain.kr was already rewritten! (to domain.com)

The trick is to reverse those blocks.

So all in all you would get:


Options +FollowSymlinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain\\.kr$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .? http://domain.com/kr [L,R=301]

RewriteCond %{HTTP_HOST} !^domain\\.com [NC]
RewriteRule .? http://domain.com%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? index.php?q=%{REQUEST_URI} [L,QSA]

Does that make sense?

Excellent tutorial … but the problem is in setting up the domains in the first place, i.e., DNS. Via cPanel, it’s pretty easy (although you may need SysAdmin help if your cPanel won’t allow you to select the Add-on domain’s DocumentRoot.

Regards,

DK