laravel web.php,laravel web服务器配置

本文介绍了如何在Apache和Nginx服务器上配置Laravel框架,以便隐藏index.php,实现优雅的URL。对于Apache,需要启用mod_rewrite模块并使用.htaccess文件;对于Nginx,需在站点配置中设置try_files指令,将所有请求导向index.php前端控制器。
摘要由CSDN通过智能技术生成

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值