【问题标题】:Configure Laravel as Subdirectory of Wordpress on IIS在 IIS 上将 Laravel 配置为 Wordpress 的子目录
【发布时间】:2018-02-12 04:42:59
【问题描述】:

由于我无法控制的原因,我需要在 IIS 10 中将 Laravel 实例作为 WordPress 的子目录运行。我在 web.config <system.webServer><rewrite><rules /> 配置中尝试了所有可能的选项,但我只能得到一个或另一个工作。如果我为 WordPress 保留重写规则,Laravel 不起作用(WordPress 以 404 响应),如果我禁用 WordPress 规则,Laravel 可以工作,但显然 WordPress 不能。

这是站点根目录(WordPress 所在的位置)的配置:

<rewrite>
  <rules>           
        <rule name="WordPress" patternSyntax="Wildcard">
            <match url="*" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />

                <!-- Laravel App -->
                <add input="{REQUEST_URI}" pattern="^/laravel" negate="true" />
            </conditions>
            <action type="Rewrite" url="index.php" />
        </rule>
    </rules>
</rewrite>

感谢 SO 搜索,我发现了这个 (IIS Url Rewrite not working with nested WP installs),它让我在 Laravel 应用程序的子文件夹中放置了一个嵌套的 web.config,其中包含以下内容:

<rewrite>
        <rules>
            <clear />
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="/(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="public/index.php/{R:1}" />
                </rule>
        </rules>
    </rewrite>

奇怪的是,有了这些,它几乎可以工作了——只要我访问 site.com/laravel/public,所有相关的视图都可以正常工作,并且一切都可以很好地协同工作。我似乎无法从 URL 中获取 /public,这显然是不可用的。

有什么建议吗?我确定它很接近,但我无法弄清楚。

【问题讨论】:

    标签: php wordpress iis laravel-5 url-rewriting


    【解决方案1】:

    知道了!我还是 Laravel 的新手,所以我并没有早点意识到这一点,但我遇到了一些问题。我的根web.config 文件设置正确,但是嵌套的web.config 需要有一个match url="*",如下所示:

    <rewrite>
            <rules>
                <clear />
                    <rule name="Imported Rule 2" stopProcessing="true" patternSyntax="Wildcard">
                       <match url="*" ignoreCase="false" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="public/index.php/{R:1}" />
                    </rule>
            </rules>
    

    完成后,它们都可以正常工作,但是如果在 URL 上不包含 /public,我会收到 404 错误 - Laravel 的 404,特别是这意味着它可以正常工作,只是不知道我在请求什么。作为一个总的猜测,我查看了我的路线并在它们前面添加了子目录,突然它们都工作了!示例:

    Route::get('/laravel/entities', 'EntitiesController@index');
    

    我不是特别喜欢将根添加到所有路由中;然而,我们的情况非常独特,所以这两个人甚至可以很好地配合,这令人印象深刻。

    【讨论】:

    • 对我来说,最重要的规则是 &lt;clear /&gt; - 这样,Laravel 附带的默认 web.config 就可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多