Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced HTACCESS Subdomain Redirection (foo.dom2.com to foo.dom1.com)

I have domain1.tld and domain2.tld. Domain1.tld is the primary domain name and domain2.tld is merely an alias. I have domain2.tld successfully redirected to domain1.tld, via HTACCESS, but I want all subdomains on domain2.tld to likewise redirect to their parallel subdomain on domain1.tld, dynamically.

For example: bla.domain2.tld should redirect to bla.domain1.tld, and foo.domain2.tld should redirect to foo.domain1.tld.

This solution needs to be dynamic, not hard-coded to specific subdomains.

Haha, I guess what I'm writing is a catch-all, one-to-one, subdomain-inclusive, alias domain redirection script in HTACCESS.

Information:

  • I am on Bluehost, without access to VHOSTS, HTTPD.CONF, etc.

  • I have a Wordpress subdomain network (multisite). This means my domain1.tld subdomains are virtual. Wordpress handles this, so as long I can get foo.domain2.tld fully redirected to foo.domain1.tld, Wordpress should be none-the-wiser and work properly. Right?

  • Wordpress has the "Professional Domain Mapping" plugin. This allows the sites on my network to have their own domain names. But the issue at hand only concerns sites on my network that don't have their own domain names, and thus utilize subdomains on my primary domain (domain1.tld).

  • Thus, I have a wildcard subdomain set up for domain1.tld.

  • But I also have a wildcard subdomain set for domain2.tld, just to make sure that all subdomains on domain2.tld are going to the same IP address as domain1.tld: my thinking being that then these subdomains should be readable by the HTACCESS in my root folder.

This is the rule in my HTACCESS that I'm expecting to do this:

RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+\.)?domain2\.com$ [NC]
RewriteRule ^(www\.)?([a-z0-9-]+\.)?(.*) http\:\/\/%2domain1\.com\/ [L]

But this isn't working. What am I missing?

I have also tried using an environmental variable, like this:

RewriteCond %{HTTP_HOST} ^(www\.)?[E=subdm:([a-z0-9-]+\.)]?domain2\.com [NC]
RewriteRule ^(.*) http\:\/\/%{ENV:subdm}domain1\.com\/ [L]

...but this just plains breaks both domains. :D

I'm relatively proficient with regular expressions, hence how comfortable I am working with HTACCESS, but I'm actually quite new to the world HTACCESS. I need another pair of eyes!

↓ UPDATE (2011-11-08 9:33am EST) ↓

I tried the first solution Jon Lin suggested, and had to edit it slightly. Originally, it wouldn't handle domain2.com without a subdomain until I put the period inside the second match parenthesis pair and followed the pair with a question mark.

As you can see below, I have gone ahead and provided the entire contents of my HTACCESS, in case you can tell that something is conflicting.

Currently redirecting properly with the suggested RewriteRule: www.domain2.com, domain2.com, and domain2.com/foo Currently NOT redirecting with the suggested RewriteRule: foo.domain2.com

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php

Options +SymLinksIfOwnerMatch
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+\.)?domain2\.com$ [NC]
RewriteRule ^(.*)$ http\:\/\/%2domain1\.com\/$1 [L,R]

# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
# END WordPress
like image 630
David Michael Gregg Avatar asked Oct 09 '22 13:10

David Michael Gregg


1 Answers

You're on the right track with %{HTTP_HOST} but you need to use backreferences (%1, %2, etc) to access the match against it. You want to do something like this:

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain2\.com$ [NC]
RewriteRule ^(.*)$ http://%2.domain1.com/$1  [L,R]

Note that if you try to access: http://www.sub.domain2.com/foo it will redirect you to http://sub.domain1.com/foo, The "www." of the original request is gone. If you want the www, change the rule to:

RewriteRule ^(.*)$ http://%1%2.domain1.com/$1  [L,R]
like image 188
Jon Lin Avatar answered Oct 13 '22 10:10

Jon Lin