ThinkPHP框架里隐藏index.php

本文所写的配置在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;
}

  摘自:http://www.jb51.net/article/82276.htm

时间: 2024-08-11 03:28:51

ThinkPHP框架里隐藏index.php的相关文章

ThinkPHP框架里隐藏index.php总结

本文所写的配置在ThinkPHP3.2.2上测试过.按理也兼容其它版本.如果你正在学习ThinkPHP框架,一定能有所收获. 首先修改配置文件: 'URL_CASE_INSENSITIVE'  =>  true,   // 默认false 表示URL区分大小写 true则表示不区分大小写'URL_MODEL'             =>  2,       // URL访问模式,可选参数0.1.2.3,代表以下四种模式: // 0 (普通模式); 1 (PATHINFO 模式); 2 (RE

ThinkPHP中如何隐藏index.php

一.修改apache中httpd.conf文件 1. 找到#LoadModule rewrite_module modules/mod_rewrite.so,将前面"#"去掉: 2.找到所有AllowOverride None,将其改为AllowOverride All: 二.在自己项目中修改配置文件(Con/config.php),修改URL模式为2 'URL_MODEL'             => 2,       // URL访问模式,可选参数0.1.2.3,代表以下四

ThinkPHP 下如何隐藏index.php

最近一直在做孕妈团的项目,因为部署到实际项目中出现了链接打不开的情况,要默认添加index.php才能正常访问. 当时忘了是Tinkphp的URL重写模式:以后遇到相同问题,首先要想到URL重写模式. httpd.conf配置文件中加载了mod_rewrite.so模块 AllowOverride None 将None改为 All 把下面的内容保存为.htaccess文件放到应用入口文件的同级目录下 <IfModule mod_rewrite.c> Options +FollowSymlink

ThinkPHP框架设计与扩展总结

详见:http://www.ucai.cn/blogdetail/7028?mid=1&f=5 可在线运行查看效果哦 导言:ThinkPHP框架是国内知名度很高应用很广泛的php框架,我们从一些简单的开发示例中来深入了解一下这个框架给我们带来的开发便捷性,以及游刃有余的扩展设计.同时也从源码分析的角度看看框架的一些不足,尽量做全面客观的评价.这里假设大家已经使用过ThinkPHP框架,基本使用方法请参考官方文档. 一.框架分层及url路由 框架的安装非常简单,下载后放入web服务器的目录即可,但

Ubuntu下配置ThinkPHP隐藏index.php

http://doc.thinkphp.cn/manual/url_rewrite.html 以上连接为框架手册提供的步骤,而Ubuntu下apache环境与windows及其它Linux有一定区别 1.开启mod_rewrite.so模块 使用sudo a2enmod,可配置模块管理,再输入rewrite,则开启了mod_rewrite.so模块. 2.AllowOverride 改为All sudo vim /etc/apache2/apache2.conf,开启找到AllowOverrid

CI 框架隐藏index.php-ubuntu

和朋友在做一个小网站,用到了CI框架,之前测试都是在windows上,隐藏index.php也相对比较简单.但服务器是ubuntu系统,需要配置一下,根据网上看到的一些教程,结合自己电脑的特点,记录步骤如下: 1.服务器环境: ubuntu12.04  64位 2.开启mod_rewrite模块: 修改 /etc/apache2/sites-enabled/000-default,将其中的:AllowOverride None 修改为:AllowOverride All,如下: Document

thinkphp 去掉URL 里面的index.php(?s=)

例如你的原路径是 http://localhost/test/index.php/home/goods/index.html 那么现在的地址是 http://localhost/test/home/goods/index.html 如何去掉index.php呢?1.httpd.conf配置文件中加载了mod_rewrite.so模块  //在APACHE里面去配置 #LoadModule rewrite_module modules/mod_rewrite.so把前面的警号去掉 2.AllowO

thinkphp中redirect重定向后隐藏index.php

首先,.htaccess文件要配置好隐藏index.php.系统默认生成的就行. 然后,也是最关键的一部,要在Application/Home/Conf里的config.php文件中增加如下配置: <?php return array( //'配置项'=>'配置值' 'URL_MODEL' => '2' ); 这样就好了.

CI框架 .htaccess 隐藏url中index.php的解决

CodeIgniter(以下简称"CI")是一款国外优秀的PHP轻量级MVC框架,它支持PHP4和PHP5,是开发中小型可拓展性需求高的Web应用程序的利器.目前你所见到的这个博客程序,正是采用CI进行的编写. 秉承MVC架构的思想,CI中的所有控制器都需要经过单点入口文件index.php(默认)来加载调用.也就是说,在默认情况下,所有CI开发项目的URL都形如以下这种形式: http://localhost/index.php/blog/logs/this_is_a_test_en