Laravel站点URL带/public/index.php问题求助
解决Laravel路由带public/index.php的问题
按照以下步骤逐一排查修复:
1. 启用Apache重写模块
首先确保mod_rewrite模块已启用,这是URL重写的基础:
sudo a2enmod rewrite sudo systemctl restart apache2
2. 修正虚拟主机配置
编辑你的Apache虚拟主机配置文件(通常位于/etc/apache2/sites-available/your-site.conf),确保以下配置正确:
DocumentRoot指向Laravel项目的public目录,而非项目根目录- 对应目录的
AllowOverride设置为All,允许.htaccess生效
示例配置:
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html/public <Directory /var/www/html/public> Options -Indexes +FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/laravel-error.log CustomLog ${APACHE_LOG_DIR}/laravel-access.log combined </VirtualHost>
修改后重启Apache:
sudo systemctl restart apache2
3. 确认.htaccess位置与内容
- Laravel的
.htaccess必须放在/var/www/html/public/目录下,而非项目根目录 - 你的
.htaccess内容是Laravel默认的正确配置,无需修改,确保文件权限为www-data可读取:
sudo chown www-data:www-data /var/www/html/public/.htaccess sudo chmod 644 /var/www/html/public/.htaccess
4. 修正Laravel环境配置
编辑项目根目录下的.env文件,设置正确的APP_URL:
APP_URL=http://你的服务器IP
5. 清除路由缓存
清除Laravel的路由缓存,确保新的URL配置生效:
cd /var/www/html php artisan route:clear php artisan config:clear
6. 验证项目权限
确保存储和缓存目录权限正确,避免Laravel运行异常:
sudo chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache sudo chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
完成以上步骤后,重新访问你的服务器IP,路由应该会自动去掉public/index.php前缀。
内容的提问来源于stack exchange,提问作者64Bit1990
相关产品推荐
相关产品推荐

