ThinkPHP框架里隐藏index.php


Posted in PHP onApril 12, 2016

本文所写的配置在ThinkPHP3.2.2上测试过。按理也兼容其它版本。

首先修改配置文件:

'URL_CASE_INSENSITIVE' => true, // 默认false 表示URL区分大小写 true则表示不区分大小写
'URL_MODEL' => 2, // URL访问模式,可选参数0、1、2、3,代表以下四种模式:
// 0 (普通模式); 1 (PATHINFO 模式); 2 (REWRITE 模式); 3 (兼容模式) 默认为PATHINFO 模式

Nginx

推荐:

location / {
try_files $uri $uri/ /index.php?s=$uri&$args;
}

意思是:如果第一个$uri不存在,就访问$uri/;如果$uri/还不存在,访问/index.php?s=$uri&$args。可以后面跟很多个。

try_files 
语法: try_files file1 [file2 ... filen] fallback 
默认值: 无 
作用域: location

再例如:

try_files $uri = 404

什么意思呢?uri不能成功访问,那好,那就给你个404吧。

但是在网上找到的文章大部分是这样配置的:

location / {
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
}

实际上不可行。

Apache

在根目录新建.htaccess文件:

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

IIS环境

如果你的服务器环境支持ISAPI_Rewrite的话,可以配置httpd.ini文件,添加下面的内容:
RewriteRule (.*)$ /index\.php\?s=$1 [I]

在IIS的高版本下面可以配置web.Config,在中间添加rewrite节点:

<rewrite>
<rules>
<rule name="OrgPage" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(.*)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}” matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" />
</rule>
</rules>
</rewrite>

附录

Nginx完整配置文

test.com.conf
server
{
listen 80;
server_name test.com;
index index.php index.html;
root /wwwroot/test.com/;
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
location / {
try_files $uri $uri/ /index.php?s=$uri&$args;
}
location ~ \.php
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 24h;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
if ( $fastcgi_script_name ~ \..*\/.*php ) {
return 403;
}
access_log logs/test.com_access.log main;
error_log logs/test.com_error.log notice;
}
PHP 相关文章推荐
几个学习PHP的网址
Nov 25 PHP
Discuz 5.0 中读取纯真IP数据库函数分析
Mar 16 PHP
php中session_id()函数详细介绍,会话id生成过程及session id长度
Sep 23 PHP
PHP的全局错误处理详解
Apr 25 PHP
PHP生成静态HTML文档实现代码
Jun 23 PHP
PHP实现防盗链的方法分析
Jul 25 PHP
PHP Laravel 上传图片、文件等类封装
Aug 16 PHP
微信公众平台开发教程③ PHP实现微信公众号支付功能图文详解
Apr 10 PHP
解决Laravel blade模板转义html标签的问题
Sep 03 PHP
laravel 模型查询按照whereIn排序的示例
Oct 16 PHP
基于laravel缓冲cache的用法详解
Oct 23 PHP
php 使用 __call实现重载功能示例
Nov 18 PHP
PHP 绘制网站登录首页图片验证码
Apr 12 #PHP
PHP中Restful api 错误提示返回值实现思路
Apr 12 #PHP
PHP给文字内容中的关键字进行套红处理
Apr 12 #PHP
PHP实现的通过参数生成MYSQL语句类完整实例
Apr 11 #PHP
PHP实现的浏览器检查类
Apr 11 #PHP
PHP模板引擎Smarty内建函数section,sectionelse用法详解
Apr 11 #PHP
PHP模板引擎Smarty内建函数详解
Apr 11 #PHP
You might like
基于mysql的论坛(4)
2006/10/09 PHP
用mysql触发器自动更新memcache的实现代码
2009/10/11 PHP
php算开始时间到过期时间的相隔的天数
2011/01/12 PHP
php实现的SSO单点登录系统接入功能示例分析
2016/10/12 PHP
PHP中str_split()函数的用法讲解
2019/04/11 PHP
laravel withCount 统计关联数量的方法
2019/10/10 PHP
js下关于onmouseout、事件冒泡的问题经验小结
2010/12/09 Javascript
IONIC自定义subheader的最佳解决方案
2016/09/22 Javascript
详解使用grunt完成requirejs的合并压缩和js文件的版本控制
2017/03/02 Javascript
jquery与js实现全选功能的区别
2017/06/11 jQuery
jQuery回调方法使用示例
2017/06/26 jQuery
Angular排序实例详解
2017/06/28 Javascript
vue实现图书管理demo详解
2017/10/17 Javascript
基于openlayers4实现点的扩散效果
2020/08/17 Javascript
angularjs 缓存的使用详解
2018/03/19 Javascript
React Router V4使用指南(精讲)
2018/09/17 Javascript
微信小程序如何使用canvas二维码保存至手机相册
2019/07/15 Javascript
mpvue实现微信小程序快递单号查询代码
2020/04/03 Javascript
Python3+django2.0+apache2+ubuntu14部署网站上线的方法
2018/07/07 Python
Python实现购物评论文本情感分析操作【基于中文文本挖掘库snownlp】
2018/08/07 Python
python实现录音小程序
2020/10/26 Python
opencv-python 读取图像并转换颜色空间实例
2019/12/09 Python
tensorflow 变长序列存储实例
2020/01/20 Python
Pytorch高阶OP操作where,gather原理
2020/04/30 Python
如何利用Python给自己的头像加一个小国旗(小月饼)
2020/10/02 Python
浅谈盘点5种基于Python生成的个性化语音方法
2021/02/05 Python
联想法国官方网站:Lenovo法国
2018/10/18 全球购物
Hanro官网:奢华男士和女士内衣、睡衣和家居服
2018/10/25 全球购物
德国高尔夫商店:Golfshop.de
2019/06/22 全球购物
求职自荐信
2013/12/14 职场文书
5s推行计划书
2014/05/06 职场文书
优秀的个人求职信范文
2014/05/09 职场文书
大学毕业生管理学求职信
2014/09/01 职场文书
见习报告怎么写
2014/10/31 职场文书
单位未婚证明范本
2014/11/25 职场文书
Spring Boot项目传参校验的最佳实践指南
2022/04/05 Java/Android