在 CentOS 上部署 Laravel 时,伪静态(URL Rewrite)通常是通过 Nginx 或 Apache 实现的。下面分别给出两种常见环境的配置方法,你可以按自己使用的 Web 服务器选择。
/www/wwwroot/laravel编辑 Nginx 站点配置(如 /etc/nginx/conf.d/laravel.conf 或 nginx.conf):
server {
listen 80;
server_name yourdomain.com;
root /www/wwwroot/laravel/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
✅ 关键点
try_files $uri $uri/ /index.php?$query_string;
这行就是 Laravel 的伪静态核心。
nginx -t
systemctl reload nginx
httpd -M | grep rewrite
如果没有开启:
vi /etc/httpd/conf/httpd.conf
确保:
LoadModule rewrite_module modules/mod_rewrite.so
<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /www/wwwroot/laravel/public
<Directory /www/wwwroot/laravel/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
.htaccessLaravel public/.htaccess 默认已存在,内容如下(无需修改):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
systemctl restart httpd
/publictry_filesAllowOverride Allchown -R nginx:nginx /www/wwwroot/laravel
chmod -R 755 /www/wwwroot/laravel
setenforce 0 # 临时关闭
# 或
setsebool -P httpd_read_user_content 1
如果你使用的是 宝塔 + CentOS:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
如果你愿意,可以告诉我:
我可以给你一份完全贴合你环境的配置。