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 相关文章推荐
isset和empty的区别
Jan 15 PHP
ajax在joomla中的原生态应用代码
Jul 19 PHP
php的SimpleXML方法读写XML接口文件实例解析
Jun 16 PHP
PHP的fsockopen、pfsockopen函数被主机商禁用的解决办法
Jul 08 PHP
PHP常用正则表达式集锦
Aug 17 PHP
thinkphp特殊标签用法概述
Nov 24 PHP
php+jQuery.uploadify实现文件上传教程
Dec 26 PHP
一个完整的php文件上传类实例讲解
Oct 27 PHP
CI框架常用方法小结
May 17 PHP
浅谈php中curl、fsockopen的应用
Dec 10 PHP
浅谈PHP表单提交(POST&amp;GET&amp;URL编/解码)
Apr 03 PHP
PHP有序表查找之插值查找算法示例
Feb 10 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
深入理解:XML与对象的序列化与反序列化
2013/06/08 PHP
字符串长度函数strlen和mb_strlen的区别示例介绍
2014/09/09 PHP
php读取目录及子目录下所有文件名的方法
2014/10/20 PHP
php简单获取复选框值的方法
2016/05/11 PHP
php的instanceof和判断闭包Closure操作示例
2020/01/26 PHP
JavaScript是否可实现多线程  深入理解JavaScript定时机制
2009/12/22 Javascript
js 剪切板应用clipboardData详细解析
2013/12/17 Javascript
使用nodejs、Python写的一个简易HTTP静态文件服务器
2014/07/18 NodeJs
深入浅析JavaScript面向对象和原型函数
2016/02/06 Javascript
jQuery实现的导航下拉菜单效果
2016/07/04 Javascript
vue.js实例对象+组件树的详细介绍
2017/10/20 Javascript
ES6使用export和import实现模块化的方法
2018/09/10 Javascript
layui表单提交到后台自动封装到实体类的方法
2019/09/12 Javascript
Vue+Bootstrap收藏(点赞)功能逻辑与具体实现
2020/10/22 Javascript
[01:52]2014DOTA2西雅图邀请赛 V社开大会你不知道的小秘密
2014/07/08 DOTA
解决python2.7 查询mysql时出现中文乱码
2016/10/09 Python
用Python分析3天破10亿的《我不是药神》到底神在哪?
2018/07/12 Python
python url 参数修改方法
2018/12/26 Python
解决python打不开文件(文件不存在)的问题
2019/02/18 Python
详解Python中is和==的区别
2019/03/21 Python
解决python3 requests headers参数不能有中文的问题
2019/08/21 Python
浅谈Django中的QueryDict元素为数组的坑
2020/03/31 Python
Django URL参数Template反向解析
2020/11/24 Python
纯css3显示隐藏一个div特效的具体实现
2014/02/10 HTML / CSS
英国羊绒服装购物网站:Pure Collection
2018/10/22 全球购物
Notino希腊:购买香水和美容产品
2019/07/25 全球购物
香港零食网购:上仓胃子
2020/06/08 全球购物
remote接口和home接口主要作用
2013/05/15 面试题
电子商务专业应届生求职信
2014/05/28 职场文书
元旦联欢会策划方案
2014/06/11 职场文书
写给老师的保证书
2015/05/09 职场文书
2015年基层党建工作汇报材料
2015/06/25 职场文书
干部考核工作总结
2015/08/12 职场文书
MySQL中日期型单行函数代码详解
2021/06/21 MySQL
Win11怎么进入安全模式?Windows 11进入安全模式的方法
2021/11/21 数码科技
彻底弄懂Python中的回调函数(callback)
2022/06/25 Python