Apache

Laravel 使用 public/.htaccess 文件来为前端控制器提供了隐藏 index.php 的优雅链接. Laravel 使用 Apache 作为服务器,请务必启用 mod_rewrite 模块 让服务器能够支持 .htaccess 的解析。

如果 Laravel 附带的 .htaccess 文件不起作用,尝试下面的方法替代:

Options +FollowSymLinks -Indexes
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Nginx

如果你使用 Nginx 服务器,在你的站点配置中加入以下内容,它将会将所有请求引导到 index.php 前端控制器中:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

虚拟主机完整配置(参考)

server {
    listen 80;
    server_name example.com;
    root /example.com/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
文章目录