OUR BLOG

16 Jun 2021

阿里云域名下的子目录指向二级域名的方法

说明:上面两个域名的记录值是指向同一ip的。

需求:

空间服务器设置域名指向不同的目录

方案1(.htaccess):

通过在根目录下面配置.htaccess文件即可,配置代码如下,(直接拷贝不要忘记修改域名以及目录哦~)

RewriteEngine on
#设置www指向根目录的子目录xxx
RewriteCond %{HTTP_HOST} ^www.xxx.com$
RewriteCond %{REQUEST_URI} !^/xxx/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /xxx/$1
RewriteCond %{HTTP_HOST} ^www.xxx.com$
RewriteRule ^(/)?$ xxx/ [L]

#设置test指向根目录的子目录test/dir
RewriteCond %{HTTP_HOST} ^test.xxx.com$
RewriteCond %{REQUEST_URI} !^/test/dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/dir/$1
RewriteCond %{HTTP_HOST} ^test.xxx.com$
RewriteRule ^(/)?$ test/dir/ [L]

说明:设置.htaccess需要开启并设置rewrite模块,具体请google吧~

方案2(apache配置为例):

通过在服务器上配置vhost也是可以滴。下面以apache为例,nginx自己google吧~

apache配置文件添加如下代码即可,或者在你引入的vhost.conf文件里修改也是可以的。

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
#空间子目录
DocumentRoot “/home/wwwroot/default/www/”
#域名
ServerName www.xxx.xom
ErrorLog “logs/dummy-host2.example.com-error.log”
CustomLog “logs/dummy-host2.example.com-access.log” common
</VirtualHost>

admin