IIS解决单页项目History模式的部署

455 阅读1分钟
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Credentials" value="true" />
                <add name="Access-Control-Allow-Headers" value="origin,x-requested-with,content-type" />
                <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
            </customHeaders>
        </httpProtocol>
        <rewrite>
            <rules>
                <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
                </rule>
                <rule name="mobile" stopProcessing="true">
                    <match url="^mobile\/(.*)" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/mobile/" />
                </rule>
                <rule name="Handle History Mode and custom 404/500" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

(1)规则一使http 跳转 https;
(2)规则二用于部署在mobile里的子级项目;
(3)规则三用于部署在根目录下的根级项目;
(4)顺序不可乱,将该文件放置在根目录下,名称全称:web.config;
(5)若还有问题,就有可能是缓存或者你的vue或react项目的配置异常; (6)若在nginx中两句话就可以解决,代码如下:

        location / {
                try_files $uri $uri/ /index.html;
        }

        location ^~ /mobile{
                index index.html; 
                try_files $uri $uri/ /mobile/index.html;
        }