IIS服务器,如何为Laravel 5配置url重写?

时间:2018-05-28 07:20:16

标签: php laravel iis

目前,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,所以你有任何想法来配置它以使其工作吗?

P.S。 我们无权在阵营中配置IIS服务器,我们有权做的是上传web.config

提前致谢。

2 个答案:

答案 0 :(得分:1)

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

<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>

正如我在开始时提到的,它不是一个完美的解决方案,它会重写所有网址到index.php ,所以网址不漂亮,比如,

  • a.com/login - &gt; a.com/index.php/login
  • a.com/ - &gt; a.com/index.php
  • a.com/login/index.php - &gt; 404

因为我对IIS配置/重写不是很熟悉,所以必须有一些更好的解决方案,所以改进这行配置文件的建议会很棒。

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

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

参考文献

答案 1 :(得分:0)

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