二级域名绑定子目录

通过配置.htaccess文件实现子目录绑定二级域名的方法

比方说我有个顶级(一级)域名:dshvv.com ,有一台服务器centos7的。我如何让yh.dshvv.com访问到另一个网站信息呢,注意哦,我只有一台服务器和一个顶级域名。这就是二级域名技术了

第一步:安装apche服务器

yum install httpd

截至CentOS7, mod_rewrite Apache模块默认情况下启用。 我们会验证这是与案件httpd -M标志,打印所有加载的模块列表

第二步:创建.htaccess文件

在/var/www/html目录下,创建.htaccess在默认文档根目录

touch /var/www/html/.htaccess

第三步:子目录绑定二级域名

编辑.htaccess文件,新增如下内容

# 开启功能 
RewriteEngine on 

# 你要绑定的二级域名 
RewriteCond %{HTTP_HOST} ^(yh.)?dshvv.com$ 

# 把那个子目录指向要绑定的二级域名 
# 这里以子目录blog目录为例 
RewriteCond %{REQUEST_URI} !^/yh/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /cms/$1 

#这里改成要绑定的二级域名和要绑定的子目录 
RewriteCond %{HTTP_HOST} ^(yh.)?dshvv.com$ 
RewriteRule ^(/)?$ yh/index.html [L]

当然,网上还有很多其它的写法,下边一种也比较简单,而且是配置了两个二级域名

# 开启功能 
RewriteEngine on 

# 第一个二级域名
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^wx.dshvv.com$
RewriteCond %{REQUEST_URI} !^/wx/
RewriteRule ^(.*)$ /wx/$1

#第二个二级域名
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^yh.dshvv.com$
RewriteCond %{REQUEST_URI} !^/yh/
RewriteRule ^(.*)$ /yh/$1

第四步:设置.htaccess文件生效

在/etc/httpd/conf/httpd.conf目录下,修改
找到<Directory /var/www/html>部分,然后更改AllowOverride从指令None到All :

<Directory /var/www/html>
. . .
 # 
 # AllowOverride controls what directives may be placed in .htaccess files.
 # It can be "All", "None", or any combination of the keywords:
 # Options FileInfo AuthConfig Limit
 #
 AllowOverride All
. . .
</Directory>

然后重启apche就好了,参考:
https://www.howtoing.com/how-to-set-up-mod-rewrite-for-apache-on-centos-7/
https://www.jb51.net/article/22037.htm

原文地址:https://www.cnblogs.com/dshvv/p/11163323.html