IIS服务器虚拟主机绑定子目录实现多域名多网站的配置方法

IIS服务器虚拟主机绑定子目录实现多域名多网站的配置方法

ergou
2025-03-05 / 0 评论 / 4 阅读 / 正在检测是否收录...

说明

买了一个永久虚拟主机,之前已经绑定了一个域名,看配置里面可以绑定五个域名,却没办法绑定子目录,其实也有可以绑定子目录的配置,贵了100米就没买。

iis.webp

用phpinfo()和$_SERVER['SERVER_SOFTWARE']查看了一下服务配置,发现是Windows+IIS。

<?php
phpinfo();

echo $_SERVER['SERVER_SOFTWARE'];

之前用其它家的虚拟机的时候,Apache服务器可以自己写配置后绑定子目录,所以也查了下IIS绑定服务器,发现一样可以,配置如下。

需求

一个虚拟主机绑定了三个域名。
web1.tag.gg ---> /
web2.tag.gg ---> /web2
web3.tag.gg ---> /web3
访问web2.tag.gg后显示web2下面内容。
访问web3.tag.gg后显示web3下面内容。
在网站根目录下的web.config文件中写入如下规则:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Web2 Rewrite" stopProcessing="true">
                    <match url="^(.*)" ></match>
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^web2\.tag\.gg$" ></add>
                        <add input="{REQUEST_URI}" pattern="^/web2/" negate="true" ></add>
                    </conditions>
                    <action type="Rewrite" url="/web2/{R:1}" ></action>
                </rule>
                <rule name="Web3 Rewrite" stopProcessing="true">
                    <match url="^(.*)" ></match>
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^web3\.tag\.gg$" ></add>
                        <add input="{REQUEST_URI}" pattern="^/web3/" negate="true" ></add>
                    </conditions>
                    <action type="Rewrite" url="/web3/{R:1}" ></action>
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

说明

其实最主要就是rule标签里面的配置,域名和子目录配置好了就没问题了。

<rule name="Web3 Rewrite" stopProcessing="true">
  <match url="^(.*)" ></match>
  <conditions>
    <add input="{HTTP_HOST}" pattern="^web3\.tag\.gg$" ></add>
    <add input="{REQUEST_URI}" pattern="^/web3/" negate="true" ></add>
  </conditions>
  <action type="Rewrite" url="/web3/{R:1}" ></action>
</rule>

第一个add标签配置域名,第二个add配置子目录名称,action标签配置域名的重定向目录。

0

评论 (0)

取消