【问题标题】:IIS server, how to config url rewriting for Laravel 5?IIS 服务器,如何为 Laravel 5 配置 url 重写?
【发布时间】:2018-05-28 07:20:15
【问题描述】:

目前,Uni 只为我们提供了一个 IIS 服务器来托管我们的 php 网站,我们想使用 Laravel 框架,它确实在主页上工作。但不在任何其他控制器上。

我的公共文件夹中的当前web.config 是,

<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="/{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>

我的路线是,

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

我不太了解 IIS,所以你有什么想法来配置它以使其工作吗?

附: 我们没有权限在营地配置IIS服务器,我们有权限做的是上传web.config

提前致谢。

【问题讨论】:

    标签: php laravel iis


    【解决方案1】:

    我遇到了同样的问题。经过多次尝试和研究,我意识到web.config 是不够的。 必须在 IIS 路由设置中设置根文件夹的路径,如下面的屏幕截图示例所示。。否则我无法解决路由问题。

    【讨论】:

    • 谢谢,这对我来说是个问题,我没有访问服务器的权限。无论如何,非常感谢您分享您的经验。
    • 如果您无法访问服务器,那么一种可能的选择是将所有链接替换为路由功能。在web.php 中为所有路线命名。然后在所有链接/操作中,而不是 href="/accounts",使用 href="{{ route('accounts') }}"。这可能会有所帮助。我记得这样的链接是有效的,但当时我不明白它们是如何工作的,所以我选择了之前提出的全局解决方案。
    【解决方案2】:

    最后我解决了这个问题,这不是一个完美的方法,但它确实有效。

    <rule name="rewrite all requests to index.php" stopProcessing="true">
         <match url="^(.*)$" ignoreCase="false" />
         <conditions logicalGrouping="MatchAll">
             <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
         </conditions>
         <action type="Rewrite" url="index.php" appendQueryString="true" />
    </rule>
    
    <rule name="Redirect / to index.php" stopProcessing="true">
         <match url="^" ignoreCase="false" />
         <action type="Rewrite" redirectType="Permanent" url="index.php" />
    </rule>
    

    正如我一开始提到的,它不是一个完美的解决方案,它将所有的url重写为index.php,所以url并不漂亮,比如,

    • a.com/login -> a.com/index.php/login
    • a.com/ -> a.com/index.php
    • a.com/login/index.php -> 404

    因为我对 IIS config/rewrite 不是很熟悉,所以肯定有更好的解决方案,所以改进这几行配置文件的建议会很好。

    无论如何,它在您没有设置 IIS 服务器的权限的情况下工作。

    它应该使 Laravel 项目可以在任何 IIS 服务器上运行,而无需更改服务器的设置。

    参考

    【讨论】:

      【解决方案3】:

      在 public 文件夹或 index.php 所在的位置创建一个名为 web.config 的文件:

      <configuration>
          <system.webServer>
              <rewrite>
                  <rules>
                      <rule name="Imported Rule 1" stopProcessing="true">
                          <match url="^" ignoreCase="false" />
                          <conditions logicalGrouping="MatchAll">
                              <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
        • 1970-01-01
        • 1970-01-01
        • 2017-03-07
        • 1970-01-01
        • 2014-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多