Laravel项目部署到WHM服务器后无法访问非首页路由求助
解决Laravel部署到WHM后非首页路由404的问题
检查Apache mod_rewrite模块状态
- 登录WHM面板,进入
软件 > Apache模块,确认mod_rewrite已勾选启用。未启用则勾选后重启Apache服务。
配置Laravel项目的.htaccess文件
- 确保项目根目录下存在
.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>
- 设置
.htaccess权限为644,并确保文件所属用户/组与Web服务器运行用户一致(如apache:apache)。
配置WHM虚拟主机(Apache环境)
- 进入WHM的
域名 > 管理Apache虚拟主机,找到对应域名的配置项:- 确认
DocumentRoot指向Laravel项目的public目录,例如:DocumentRoot "/home/yourusername/public_html/public" - 在虚拟主机配置块中添加如下内容,确保.htaccess规则生效:
<Directory "/home/yourusername/public_html/public"> AllowOverride All Require all granted </Directory> - 确认
- 修改完成后重启Apache服务。
Nginx环境下的配置调整(若使用Nginx)
- 找到对应域名的Nginx配置文件,确保包含以下重写规则:
location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm-yourusername.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; }
- 确认
root指向Laravel的public目录,例如:root /home/yourusername/public_html/public; - 修改后重启Nginx服务。
清除Laravel缓存
- 通过SSH终端进入项目根目录,执行以下命令:
php artisan route:clear php artisan config:clear php artisan cache:clear
SELinux状态检查(若服务器启用SELinux)
- 执行
getenforce查看状态,若显示Enforcing,可临时关闭测试:setenforce 0,验证路由是否恢复正常。 - 若临时关闭后正常,设置正确的SELinux上下文:
chcon -R -t httpd_sys_rw_content_t /home/yourusername/public_html/storage chcon -R -t httpd_sys_rw_content_t /home/yourusername/public_html/bootstrap/cache
内容的提问来源于stack exchange,提问作者Sahil Verma
相关产品推荐
相关产品推荐

