Home  >  Article  >  Backend Development  >  Use PHP parsing to implement secondary domain name redirection

Use PHP parsing to implement secondary domain name redirection

WBOY
WBOYOriginal
2016-07-30 13:29:431225browse

After registering a domain name, it is often necessary to implement different second-level domain names to access different nodes of the site.

General domain name registration agencies provide a cname resolution method, and you can define the second-level domain name to different IPs.

For example,

www.abc.com points to the main node 1.2.3.4

bbs.abc.com points to 1.2.3.4/bbs or another IP

But if the website rents hosting space, painful problems will arise.

The website only has one IP, and the website is a rented hosting space, not a hosting host. Different access nodes cannot be set directly. The hosting space can only set an entrance URL provided by the space provider

That is, the website has only one entrance, a second-level domain name It cannot be directly resolved to different subdirectories

Using PHP code can solve this problem,

Ideas

1. Define different second-level domain names, pointing to the same website entrance

2. Index on the first page of the website entrance. php, determine the domain name entered by the user, and then redirect to the website sub-contact

php global variable $_SERVER['HTTP_HOST'], you can get the domain name string currently visited by the user, and query each sub-domain name string defined by yourself in it , and then use the header function to redirect to a different page

The code is as follows

if( strpos($_SERVER['HTTP_HOST'],"china") !== false ) // Be sure to use !== cannot be used! =, otherwise false and 0 cannot be distinguished
{//
header('location:/china/indexphp');
}elseif( strpos($_SERVER['HTTP_HOST'],"bbs") !== false )
{
header('location:/bbs/forum.php');
}
else
{//Show homepage
header('location:/templets/default/index.htm');
}

Copyright statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger.

The above introduces the use of PHP parsing to implement secondary domain name redirection, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn