您现在的位置是:首页 > 建站知识建站知识

巧用伪静态解决虚拟主机不支持绑定到子目录的问题

AndyGuo2020-05-05 12:05:40建站知识5032人已围观

简介我们知道一般虚拟主机是不支持绑定到子目录的,这时候该怎么办呢。

我们知道一般虚拟主机是不支持绑定到子目录的,这时候该怎么办呢。

我们可以利用伪静态实现将域名或二级域名绑定到子目录,

如果是Apache环境,在站点根目录新建一个文件命名为:.htaccess

下面是 123.d163.net域名绑定到子目录dh

RewriteEngine On
RewriteBase /
  
# 绑定123.d163.net 到子目录dh

RewriteCond %{HTTP_HOST} ^123\.d163\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/dh/
RewriteRule ^(.*)$ dh/$1?Rewrite [L,QSA]

#可以绑定多个 只需重复上三行代码并更改一下域名、目录名 就好了  

如果是windows服务器,在站点根目录新建web.config文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?s={R:1}" appendQueryString="true" />
                </rule>
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{HTTP_HOST}" pattern="^123\.d163\.net$" />
                        <add input="{URL}" pattern="^/dh/" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="dh/{R:1}?Rewrite" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

完成以上步骤就可以实现访问123.d163.net而直接访问子目录dh了(子目录dh中有index.html或index.php)

小郭博客

标签云 更多

关闭
QQ 微信 支付宝扫一扫打赏