HTACCESS Advanced 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 just an alias. I have domain2.tld successfully redirected to domain1.tld via HTACCESS, but I want all subdomains on domain2.tld to be redirected 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 should be dynamic, not rigidly defined for certain subdomains.

Haha, I think that what I am writing is the forwarding of alias-to-all, one-to-one domains, a sub-domain including a script in HTACCESS.

information:

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

  • I have a WordPress subdomain network (multisite). This means that my domain1.tld subdomains are virtual. Wordpress handles this so that until I can completely redirect fud.domain2.tld to foo.domain1.tld, Wordpress should not be the best and work correctly. Right?

  • Wordpress has a Professional Domain Mapping plugin. This allows my network sites to have their own domain names. But the problem only applies to sites on my network that do not have their own domain names, and thus use subdomains in my primary domain (domain1.tld).

  • Thus, I have a substitution subdomain for domain1.tld.

  • But I also have a substitution subdomain for domain2.tld to make sure that all subdomains on domain2.tld go to the same IP address as domain1.tld: my thinking is that then these subdomains should read HTACCESS in my root folder.

This is the rule in my HTACCESS that I expect to do:

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

But that does not work. What am I missing?

I also tried using an environment variable, for example:

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

... but it is only the plains that break both domains .: D

I am relatively good at regular expressions, so how comfortable I work with HTACCESS, but I'm actually completely new to the world of HTACCESS. I need two more eyes!

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

I tried the first solution suggested by Jon Lin, and he had to edit it slightly. Initially, it would not have processed domain2.com without a subdomain until I put the period in the second pair of parentheses and completed a pair with a question mark.

As you can see below, I went ahead and provided all the contents of my HTACCESS, if you can say that something is contradictory.

Currently redirecting with recommended RewriteRule: www.domain2.com, domain2.com and domain2.com/foo Currently NOT redirecting with 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 
+4
source share
1 answer

You are on the right track with% {HTTP_HOST}, but you need to use backlinks (% 1,% 2, etc.) to gain access to a match with it. You want to do something like this:

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

Please note that if you try to access: http://www.sub.domain2.com/foo , it will redirect you to http://sub.domain1.com/foo , "www." initial request failed. If you want to use www, change the rule to:

 RewriteRule ^(.*)$ http://%1%2.domain1.com/$1 [L,R] 
+3
source

Source: https://habr.com/ru/post/1380118/


All Articles