You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Server部署Laravel后CSS/JS无法加载问题求助

Laravel部署Windows Server后,域名访问时CSS/JS资源返回200但空白无法加载

问题描述

将Laravel网站部署在Windows Server服务器上,执行php artisan serve命令时网站运行完全正常,但使用域名访问时,所有CSS和JS资源均无法加载。查看浏览器控制台网络请求状态,所有资源请求状态码均为200,但打开资源链接(如https://example.com/subfolder/asset/js/bootstrap/bootstrap.min.js)时仅显示空白页面,无任何报错信息。

已尝试的排查操作:

  • 为IUSRS账户授予完全权限
  • 重启服务器
  • 修改.htaccess及web.config配置文件

本地通过php artisan serve运行时一切正常,但使用localhost或域名访问时,均出现CSS和JS无法加载的情况。

现有配置文件

.htaccess配置

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
        Options +Indexes 
    # or #
    IndexIgnore *
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" env=HTTPS
</IfModule>


<IfModule mod_headers.c>
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires 0
    Header unset X-Powered-By
</IfModule>

# Blocks some XSS attacks
<IfModule mod_rewrite.c>
    RewriteCond %{QUERY_STRING} (\|%3E) [NC,OR]
    RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
    RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
    RewriteRule .* index.php [F,L]
</IfModule>

# Protect Against  Injection
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} ^(HEAD|TRACE|DELETE|TRACK) [NC]
    RewriteRule ^(.*)$ - [F,L]
    RewriteCond %{QUERY_STRING} \.\.\/ [NC,OR]
    RewriteCond %{QUERY_STRING} boot\.ini [NC,OR]
    RewriteCond %{QUERY_STRING} tag\= [NC,OR]
    RewriteCond %{QUERY_STRING} ftp\:  [NC,OR]
    RewriteCond %{QUERY_STRING} http\:  [NC,OR]
    RewriteCond %{QUERY_STRING} https\:  [NC,OR]
    RewriteCond %{QUERY_STRING} (\&lt;|%3C).*script.*(\&gt;|%3E) [NC,OR]
    RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|%3D) [NC,OR]
    RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [NC,OR]
    RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|&lt;|&gt;|ê|&quot;|;|\?|\*|=$).* [NC,OR]
    RewriteCond %{QUERY_STRING} ^.*(%24&amp;x).* [NC,OR]
    RewriteCond %{QUERY_STRING} ^.*(%0|%A|%B|%C|%D|%E|%F|127\.0).* [NC,OR]
    RewriteCond %{QUERY_STRING} ^.*(globals|encode|localhost|loopback).* [NC,OR]
    RewriteCond %{QUERY_STRING} ^.*(request|select|insert|union|declare).* [NC]
    RewriteCond %{HTTP_COOKIE} !^.*WordPress_logged_in_.*$
    RewriteRule ^(.*)$ - [F,L]
</IfModule>


<IfModule mod_headers.c>
    Header set Expect-CT enforce,max-age=2592000,report-uri="https://foo.example/report"
</IfModule>

    
# Security Headers
<IfModule mod_headers.c>
    Header set X-XSS-Protection "1; mode=block"
    # Header set X-Frame-Options "SAMEORIGIN"
    Header set X-Frame-Options Deny
    Header set X-Content-Type-Options "nosniff"
   
    Header set Referrer-Policy "same-origin"
    
</IfModule>

web.config配置

<configuration>
    <system.webServer>
        <defaultDocument>
            <files>
                <clear />
                <add value="index.php" />
                <add value="default.aspx" />
                <add value="Default.htm" />
                <add value="Default.asp" />
                <add value="index.htm" />
                <add value="index.html" />
            </files>
        </defaultDocument>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^(.*)/$" ignoreCase="false" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Redirect" redirectType="Permanent" url="/{R:1}" />
                </rule>
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="^" ignoreCase="false" />
                    <conditions>
                        <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" />
                </rule>
            </rules>
        </rewrite>
        <httpErrors errorMode="Detailed" />
    </system.webServer>
</configuration>

解决方案
  1. 修正资源路径引用

    • 确保Blade模板中使用Laravel官方辅助函数生成资源路径,比如{{ asset('asset/js/bootstrap/bootstrap.min.js') }},而非硬编码相对路径。
    • 检查.env文件的APP_URL配置,必须填写完整的域名(含子目录,如https://example.com/subfolder),asset函数依赖此配置生成正确的资源URL。
  2. 调整web.config重写规则(适配子目录)
    当前规则未考虑子目录部署场景,导致静态资源被错误转发到index.php处理,返回空白内容。修改以下两处:

    <!-- 修正规则1的跳转路径 -->
    <rule name="Imported Rule 1" stopProcessing="true">
        <match url="^(.*)/$" ignoreCase="false" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
        </conditions>
        <action type="Redirect" redirectType="Permanent" url="/subfolder/{R:1}" />
    </rule>
    
    <!-- 修正规则2的重写路径 -->
    <rule name="Imported Rule 2" stopProcessing="true">
        <match url="^" ignoreCase="false" />
        <conditions>
            <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="/subfolder/index.php" />
    </rule>
    

    注意将/subfolder替换为你实际的子目录名称。

  3. 验证IIS静态文件支持

    • 打开服务器管理器,确认已安装Web服务器(IIS)下的静态内容角色服务,否则IIS无法正确解析CSS/JS文件。
    • 检查网站的「处理程序映射」,确保.css、.js后缀的处理程序为StaticFile,而非被PHP处理程序接管。
  4. 清理Laravel缓存
    执行以下命令清除配置、视图及应用缓存:

    php artisan config:clear
    php artisan view:clear
    php artisan cache:clear
    
  5. 简化.htaccess配置
    Windows Server下IIS优先使用web.config,.htaccess仅在安装URL Rewrite模块并启用支持时生效。若未启用该模块,建议移除或简化.htaccess,避免规则冲突。

内容的提问来源于stack exchange,提问作者sql query

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 03:07:13