You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置IIS服务器实现Laravel 5的URL重写?(非首页路由失效)

Fixing Laravel Route Issues on IIS

Hey, I’ve dealt with this exact problem when hosting Laravel on IIS before—your root route works, but other routes like /home don’t because the URL rewrite rules aren’t properly passing the full request path to Laravel’s index.php. Since you only have access to modify the web.config file, here’s a tested configuration to replace your current one:

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <!-- Redirect URLs with trailing slashes to clean version -->
                <rule name="Remove Trailing Slash" stopProcessing="true">
                    <match url="(.*)/$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Redirect" redirectType="Permanent" url="{R:1}" />
                </rule>
                <!-- Rewrite all non-file/directory requests to index.php with full path -->
                <rule name="Laravel Route Handler" stopProcessing="true">
                    <match url="^" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:0}" />
                </rule>
            </rules>
        </rewrite>
        <!-- Ensure IIS processes PHP files correctly (adjust scriptProcessor path if needed) -->
        <handlers>
            <add name="PHP FastCGI" path="*.php" verb="*" modules="FastCgiModule" scriptProcessor="C:\PHP\php-cgi.exe" resourceType="Either" requireAccess="Script" />
        </handlers>
    </system.webServer>
</configuration>

Key Changes & Explanations:

  • The critical fix is in the second rule’s <action>: instead of rewriting to just index.php, we use index.php/{R:0}. This passes the full requested URL path (like /home) to Laravel’s front controller, so Laravel’s router can properly parse and route the request.
  • Added the <handlers> section to ensure IIS recognizes and processes PHP files correctly. If your school’s server already has PHP configured, you might not need this—but including it won’t hurt. If you run into errors, you can safely remove this section.
  • Cleaned up rule names and condition logic to make the configuration easier to read and maintain.

Quick Pre-Checks:

  • Confirm your Laravel project’s public folder is set as the root directory of your IIS site—this is essential for index.php to be accessible and for the rewrite rules to function as intended.
  • Verify the server has the IIS URL Rewrite Module installed. Since your root route loads, this is likely already enabled, but if not, you’ll need to ask your school’s admin to install it (it’s a required component for Laravel routing on IIS).

After replacing your web.config, try accessing /home or other routes—they should now load correctly!

内容的提问来源于stack exchange,提问作者tim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:33:48