.Htaccess Rewriterule: Two Domains Using Same Server and Directory

.htaccess RewriteRule: two domains using same server and directory

Add a RewriteCond directive, which defines the conditions for the following RewriteRule.

So for example:

RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^([0-9]+)/?$ project.php?id=$1 [NC,L] # Handle project requests

RewriteCond %{HTTP_HOST} ^mydomain\.cz
RewriteRule ^([0-9]+)/?$ project_cz.php?id=$1 [NC,L] # Handle project requests

Manage two domains pointing to one hosting with htaccess

Your question is probably a duplicate but I can't find one that uses adjacent dirs (as I think you're describing), nor one with a solution I'd use, myself. Try this in your root .htaccess:

RewriteEngine on
# capture the original request so you never have trouble with (un)escaping
RewriteCond %{THE_REQUEST} \S+\s+(\S*)
RewriteRule ^ - [E=REQ:%1]
# ensure the domain goes to a dir with the same name (ignoring www)
# get domain
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)
# see if first dir in url doesn't match domain
RewriteCond %1 =!^(?>([^|]+)\|\1)$
# capture first dir (if any) to check against above conditions and pass through as new url if not prefixed with domain dir
RewriteRule ^[^/]* %1%{ENV:REQ} [NE,DPI,PT]

Your dir structure would then be

docroot/
domain1.com/
whatever
domain2.net/
whatever
.htaccess

You should then be able to have normal .htaccess directives in each subdir, if you wish. Ensure you use RewriteBase / in those dirs.


Re-edit That just makes your situation more like most others'.

RewriteEngine on
RewriteBase /
# capture the original request so you never have trouble with (un)escaping
RewriteCond %{THE_REQUEST} \S+\s+(\S*)
RewriteRule ^ - [E=REQ:%1]
# ensure the domain goes to the required dir
# get domain
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)
RewriteRule ^ - [E=DOM:%1]
# explicitly set dir per host
RewriteCond %{ENV:DOM} =sub.domain1.com [NC]
RewriteRule !^sub/ sub%{ENV:REQ} [NE,DPI,L]
RewriteCond %{ENV:DOM} =domain2.com [NC]
RewriteRule !^d2/ d2%{ENV:REQ} [NE,DPI,L]

# allow domain1.com to proceed to root (any other rules go below)
# rules must still exclude subdirectories for other domains, e.g.:
RewriteRule ^(?!sub/|d2/)([^/]+)/([^/.]+)$ foo.php?bar=$1&baz=$2 [NE,B,L,DPI]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(?:sub/|d2/|index\.php$) index.php [L,DPI]

# after all other rules, emulate DirectorySlash so that Apache does not naively insert hidden directory into public URL
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (?>.*)(?<!/) %{ENV:REQ}/ [L,DPI,R]

If you're unsure of any regex, paste it into regex101.com (with the g flag off) and look at the explanation.

htaccess - hosting multiple domains on one shared server,

Untested code: I believe you can do the following

RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteRule ^site2/(.*)$ http://site1.com/$1 [R=301]

This will (or should) redirect the client from http://site1.com/site2/my/awesome/page.php to http://site2.com/my/awesome/page.php.

htaccess: multiple redirections depending on domain name

For your A and B domains, use:

RewriteCond %{HTTP_HOST} ^(www\.)?(a|b).com$ [NC]
RewriteRule ^(.*)$ /root/ [L]

The (www\.)? allows you to use both with and without www (the ? means "the preceding block zero or one time").

(a|b) means "a or b". You can add more domains in there using more pipes (a|b|d|e)

For the other one:

RewriteCond %{HTTP_HOST} ^(www\.)?c.com$ [NC]
RewriteRule ^(.*)$ /root/folder1/ [L]

If you want www.A.com/hello.php to redirect to /root/hello.php then add $1 after the /root/ in the RewriteRule (goes for both rules)

[AND] does not exist because it is implicit when you use multiple RewriteConds. Something like this:

RewriteCond %{HTTP_HOST} ^www.a.com$
RewriteCond %{HTTP_HOST} ^www.b.com$

already means has [AND] implied and thus will never work (because a domain can never be a.com and b.com at the same time).


update
Answer to your new question:

"Any domain other than C.com and other than D.com redirects (301) to www.A.com. A.com points to the root folder of the server anyway and that is set in DNS."

RewriteCond %{HTTP_HOST} !^(www\.)?(c|d).com$ [NC]
RewriteRule ^(.*)$ http://www.a.com/$1 [L,R=301]

"Domain www.C.com points to the folder 'folder1' on the server. Can it be set in htaccess?"

RewriteCond %{HTTP_HOST} ^www.c.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder1/
RewriteRule ^(.*)$ /folder1/$1 [L,R]

"Now domains C.com, www.D.com and D.com redirects to www.C.com."

RewriteCond %{HTTP_HOST} ^c.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?d.com$ [NC]
RewriteRule ^(.*)$ http://www.c.com/$1 [L,R=301]

.htaccess multiple-domain rewrite

Well, it seems impossible to do neatly through htaccess..

O well, I "fixed" it. My fix within the CodeIgniter index.php: I replaced the declaration of the variable $application_folder with the code beneath.

define('DOMAIN', preg_replace(
"/^(www.|local.)?([^.]+).[^.]+$/i", "\\2",
$_SERVER['HTTP_HOST']
));

if ( DOMAIN == 'domain2')
$application_folder = '../application/domain2';
else
$application_folder = '../application/domain1';

I also made a minor change to the system "url_helper". Added this "static_url()" function, returning the URI to the path I save images/CSS/js etc.

if ( ! function_exists('static_url'))
{
function static_url()
{
return base_url().'static/'.DOMAIN.'/';
}
}

Only minor thing I've got to figure out is how to split up things like robots.txt



Related Topics



Leave a reply



Submit