简体   繁体   中英

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. But not on any other controllers.

Current web.config in my public folder is,

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

PS We do not have rights to config IIS server in camps, what we have rights to do is upload the web.config .

Thanks in advance.

I got into the same issue. After many tries and researches, I realized that web.config is not enough. The path to the root folder has to be set in IIS route settings as shown in below screenshot example. 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,

  • a.com/login -> a.com/index.php/login
  • a.com/ -> a.com/index.php
  • 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.

Anyway it worked in the situation that you do not have permission to set the IIS server.

It should make Laravel project or any PHP project running on any IIS server without changing the setting of server.

Refs

Create a file with this name web.config on public folder or where index.php is with this configuration :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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