Laravel部署在子目录/blog下文章详情页404问题求助
解决Laravel子目录部署后详情页404问题
你的问题核心是服务器未正确配置URL重写规则,导致Laravel的路由无法被正确解析,尤其是在子目录部署场景下。以下是针对性的解决方案:
1. Apache环境下的配置调整
- 首先确认服务器已启用
mod_rewrite模块,执行a2enmod rewrite命令开启后,重启Apache服务。 - 修改项目根目录(服务器
/blog目录)下的.htaccess文件,适配子目录路径:
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On RewriteBase /blog/ # 将请求转发到public目录 RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L] </IfModule>
- 保持
/blog/public目录下的.htaccess为Laravel默认规则:
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On # 直接返回静态文件请求 RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ ^$1 [N] # 非静态请求转发到index.php处理 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
2. Nginx环境下的配置调整
修改站点配置文件的server块,添加子目录路由规则:
server { listen 443 ssl; server_name brandztory.me; ssl_certificate /path/to/your/cert.pem; ssl_certificate_key /path/to/your/key.pem; root /path/to/your/server/blog/public; index index.php index.html; location /blog { try_files $uri $uri/ /blog/index.php?$query_string; } location ~ \.php$ { fastcgi_pass unix:/run/php/php8.1-fpm.sock; # 根据实际PHP版本调整 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
修改完成后执行systemctl restart nginx重启服务。
3. Laravel环境配置修正
打开项目根目录的.env文件,更新APP_URL为子目录地址:
APP_URL=https://brandztory.me/blog
同时清理路由缓存:
php artisan route:clear
4. 验证slug合法性
检查服务器上json/blog.json文件中judul_rtikel_slug字段,确保无空格、未转码中文等特殊字符,避免URL解析异常。
内容的提问来源于stack exchange,提问作者Firqy 17
相关产品推荐
相关产品推荐

