问题
I have a site that is up and running and have gotten another domain and need redirect any visit to exampleA.com or any sub-domain of it, to a page where I can check the requested page's sub-domain to a dynamic list of sites and if its a match send it to that sub-domain on exampleB.com, and if not send it to the main site on exampleB.com. there will be no site at all on exampleA.com is is just a shorter version of the main domain. The main site is a wordpress site.
What I have.
I have exampleA.com and exampleB.com with exampleB.com having sub-domains.
What I need.
redirect *.exampleA.com to exampleB.com/somePage/?from=*
After it gets to exampleB.com/somePage/?from=* I can check the $_GET info. but I'm not sure how to set the arbitrary sub-domain to the $_GET
回答1:
You'd need a few things:
a) exampleA.com's DNS setup must be configured to allow wildcard subdomains:
*.exampleA.com. 3600 IN A x.x.x.x
b) exampleA.com's web server configuration must allow wildcard host matching
<VirtualHost ...>
ServerName exampleA.com
ServerAlias *.exampleA.com
</VirtualHost
c) exampleA.com's default document would be a simple PHP script that extracts the hostname in use and issues a redirect to exampleB with the extracted host name.
<?php
$requested_host = $_SERVER['HTTP_HOST'];
$parts = explode('.', $requested_host);
array_pop($parts); // com
array_pop($parts); // exampleA
$requested_subhost = implode('.', $parts);
header("Location: http://exampleB.com/?from=$requested_subhost");
回答2:
When you match the domain name against the list (which you can get from $_SERVER['HTTP_HOST']), you can then redirect the user dynamically using PHP's header('Location: http://example.com/').
来源:https://stackoverflow.com/questions/6735685/redirect-arbitrary-subdomain-to-page-with-get-info