Resolve the domain name to the secondary directory of the Apache virtual host

need

A virtual host does not have as many ways to play as a server, and there is no way to modify some configurations through the command line. Basically, there is only one control panel to configure basic things. If you want to resolve multiple domain names to this virtual host, so that each secondary directory becomes the root directory of the current domain name, it can actually be done.

step

1. Resolve the domain name normally to ensure that the domain name can access the root directory normally. 2. Create a  file
in the root directory of the virtual host .htaccess

.htaccess file rules

The .htaccess file is the configuration file of the Apache host, through which we can implement some forwarding rules. The following rule is directly copied into the .htaccess file and saved.

This rule is to bind weixin.qq.com to the weixin directory under the secondary directory

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# 绑定weixin.qq.com到二级目录weixin
RewriteCond %{HTTP_HOST} ^weixin\.qq\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/weixin/
RewriteRule ^(.*)$ weixin/$1?Rewrite [L,QSA]

</IfModule>

 If you want to bind multiple domain names, just copy a layer of configuration, for example:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# 绑定weixin.qq.com到二级目录weixin
RewriteCond %{HTTP_HOST} ^weixin\.qq\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/weixin/
RewriteRule ^(.*)$ weixin/$1?Rewrite [L,QSA]

# 绑定tieba.baidu.com到二级目录tieba
RewriteCond %{HTTP_HOST} ^tieba\.baidu\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/tieba/
RewriteRule ^(.*)$ tieba/$1?Rewrite [L,QSA]

# 绑定www.taobao.com到二级目录taobao
RewriteCond %{HTTP_HOST} ^www\.taobao\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/taobao/
RewriteRule ^(.*)$ taobao/$1?Rewrite [L,QSA]

</IfModule>

Guess you like

Origin blog.csdn.net/weixin_39927850/article/details/127913193