简体   繁体   English

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

[英]IIS server, how to config url rewriting for Laravel 5?

Currently, the Uni only offered us an IIS server to host our php website, we want to use Laravel framework, and it did work on home page.目前,Uni 只为我们提供了一个 IIS 服务器来托管我们的 php 网站,我们想使用 Laravel 框架,它确实可以在主页上运行。 But not on any other controllers.但不在任何其他控制器上。

Current web.config in my public folder is,我公用文件夹中的当前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>

My route is,我的路线是

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

Auth::routes();

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

I do not know about the IIS very well, so do you get any ideas to config it to make it work?我不太了解 IIS,所以你有什么想法来配置它以使其工作吗?

PS We do not have rights to config IIS server in camps, what we have rights to do is upload the web.config . PS 我们无权在营地配置 IIS 服务器,我们有权做的是上传web.config

Thanks in advance.提前致谢。

I got into the same issue.我遇到了同样的问题。 After many tries and researches, I realized that web.config is not enough.经过多次尝试和研究,我意识到web.config是不够的。 The path to the root folder has to be set in IIS route settings as shown in below screenshot example.必须在 IIS 路由设置中设置根文件夹的路径,如下面的屏幕截图示例所示。 IIS上的laravel路径 . . Otherwise I wasn't able to resolve the routing issue.否则我无法解决路由问题。

Finally I solved this, it is not a perfect way, but it did work.最后我解决了这个问题,这不是一个完美的方法,但它确实有效。

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

As I mentioned in the beginning, it is not a perfect solution, it rewrite all urls to index.php , so the URLs are not pretty, like,正如我在开头提到的,这不是一个完美的解决方案,它将所有 url 重写为 index.php ,所以 URL 并不漂亮,比如,

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

Because I am not very familiar with IIS config/rewrite, there must be some better solution, so improving advice of this lines of config file will be great.因为我对 IIS config/rewrite 不是很熟悉,所以肯定有更好的解决方案,所以改进这几行配置文件的建议会很棒。

Anyway it worked in the situation that you do not have permission to set the IIS server.无论如何,它在您无权设置 IIS 服务器的情况下工作。

It should make Laravel project or any PHP project running on any IIS server without changing the setting of server.它应该使 Laravel 项目或任何 PHP 项目在任何 IIS 服务器上运行,而无需更改服务器的设置。

Refs参考文献

Create a file with this name web.config on public folder or where index.php is with this configuration :在 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>

This works for me (including ssl redirect and webp image format) :这对我有用(包括 ssl 重定向和 webp 图像格式):

<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>
            <rule name="ssl_redirection" stopProcessing="false">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
            </rule>
        </rules>
    </rewrite>
    <staticContent>
        <mimeMap fileExtension=".webp" mimeType="image/webp" />
    </staticContent>
</system.webServer> </configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM