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
相关产品推荐
相关产品推荐

