Redirect arbitrary subdomain to page with $_get info

岁酱吖の 提交于 2019-12-13 00:54:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!