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

Laravel站点URL优化:移除public路径与index.php

Laravel子目录站点URL优化方案

解决目标

直接访问https://mypage/xxxx/即可打开站点,同时移除URL中的index.php

操作步骤

1. 配置xxxx根目录的.htaccess

在public_html/xxxx/下的空.htaccess文件中添加以下规则,将所有请求转发到public目录:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

2. 确认public目录的.htaccess配置

确保public_html/xxxx/public/下的默认Laravel .htaccess保持完整,默认内容如下(如果你的已经是这个就不用修改):

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

3. 修正Laravel配置文件

打开项目根目录的.env文件,修改APP_URL为:

APP_URL=https://mypage/xxxx

同时确认config/app.php中的'url'配置与.env保持一致:

'url' => env('APP_URL', 'https://mypage/xxxx'),

4. 测试验证

完成以上配置后,直接访问https://mypage/xxxx/,应该能正常打开站点,且URL中不会出现public/或index.php。如果仍有问题,检查服务器是否开启了mod_rewrite模块(Apache环境下)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 02:09:55