【问题标题】:IIS not serving .css & .js files for Laravel siteIIS 不为 Laravel 站点提供 .css 和 .js 文件
【发布时间】:2016-01-14 09:28:25
【问题描述】:

我正在处理的网站存在问题,其中 .css 会引发 404 错误,并且它已经给我带来了一天多的问题。我最近使用 IIS10 在我的机器上设置了一个本地托管的 Laravel 5 站点。我有一个存储在 /views/layouts (layout.blade.php) 中的主布局,其中包含如下样式表:

<head>
    ...

    <link href="{{URL::asset('css/bootstrap.min.css')}}" rel="stylesheet" />
    <link href="{{URL::asset('css/font-awesome.min.css')}}" rel="stylesheet" />
    <link href="{{URL::asset('css/prettyPhoto.css')}}" rel="stylesheet" />
    <link href="{{URL::asset('css/price-range.css')}}" rel="stylesheet" />
    <link href="{{URL::asset('css/animate.css')}}" rel="stylesheet" />
    <link href="{{URL::asset('css/main.css')}}" rel="stylesheet" />
    <link href="{{URL::asset('css/responsive.css')}}" rel="stylesheet" />

     ...
</head>

现在,如果我打开浏览器并导航到http://localhost/test_site/public/,我的所有视图都会显示没有任何样式。打开开发者工具有七个错误说明:

HTTP404: NOT FOUND - The server has not found anything matching the requested URI. (XHR): GET - http://localhost/test_site/public/css/<filename>.css

我很困惑为什么如果显示到 css 文件的路径是正确的,那么为什么它不会像您期望的那样呈现。我最初的想法是我的 web.config 文件有问题,我有:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <staticContent>
            <remove fileExtension=".css" />
            <mimeMap fileExtension=".css" mimeType="text/css" />

            <remove fileExtension=".js" />
            <mimeMap fileExtension=".js" mimeType="application/javascript" />

            <remove fileExtension=".jpg" />
            <mimeMap fileExtension=".jpg" mimeType="image/jpeg" />

            <remove fileExtension=".png" />
            <mimeMap fileExtension=".png" mimeType="image/png" />
        </staticContent>
        <rewrite>
            <rules>
                <rule name="Rewrite rule1 for laravel_rewrite">
                <match url=".*" />
                <conditions>
                     <add input="{laravel_rewrite:{HTTP_HOST}}" pattern="(.+)" />
                </conditions>
                <action type="Rewrite" url="{C:1}{REQUEST_URI}" appendQueryString="false" />
            </rule>
            <rule name="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="/Tutorial/public/" />
            </rule>
            <rule name="Rewrite routed access to assets(img, css, files, js, favicon)" stopProcessing="true">
                <match url="^(img|css|files|js|favicon.ico)(.*)$" />
                <action type="Rewrite" url="/Tutorial/public/{R:1}{R:2}" appendQueryString="false" />
            </rule>
        </rules>
        <rewriteMaps>
            <rewriteMap name="laravel_rewrite">
                <add key="localhost" value="/Tutorial/public" />
            </rewriteMap>
        </rewriteMaps>
    </rewrite>
</system.webServer>
</configuration> 

我最初使用 IIS 中的 URL Rewrite 模块生成了 web.config 文件(存储在公用文件夹中),但是在 &lt;staticContent&gt;...&lt;/staticContent&gt; 和我尝试解决此问题的第三条规则之间添加了。

我还认为问题可能出在处理程序映射上,特别是使用 * 通配符作为 RequestPath 的 StaticFile。我把那个留在原地,但我还用*.css*.js 定义了单独的处理程序,但没有帮助。

有人对我可以尝试的其他事情有什么建议吗?澄清一下,这里有一些关于我的设置的附加信息:

  • Windows 功能 Static Content 已启用(我还按照其他一些网站的建议禁用并重新启用它)。
  • IUSR 具有访问网站所有文件夹的适当权限。

提前感谢任何可以提供帮助的人!

【问题讨论】:

    标签: php css iis laravel-5 url-routing


    【解决方案1】:

    所以在搞砸了一些事情之后,包括为每个单独的文件类型创建新的处理程序映射等等,我删除了我的 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>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Redirect" redirectType="Permanent" url="public/{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> 
    </system.webServer>
    </configuration>
    

    现在一切都按照您的预期进行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 2012-09-21
      相关资源
      最近更新 更多