网站首页 > 技术教程 正文
在使用PHP框架(CI/TP)搭建项目时, 我们经常使用Nginx/Apache的URL Rewrite模块实现伪静态。例如, 访问URL: http://app.cn/page/13044, 同时我们也可以通过访:http://app.cn/index.php?/page/13044访问,打开的页面是一模一样的。在Nginx设置如下URL Rewrite规则就可以实现:
rewrite ^/(.*)$ /index.php?$1 last;
除了伪静态URL外,我们还可以通过其他几种URL模式访问。例如CI框架:
CI框架
CI框架中可选三种URL 模式,在application/config/config.php可以定义:
/* |-------------------------------------------------------------------------- | URI PROTOCOL |-------------------------------------------------------------------------- | | This item determines which server global should be used to retrieve the | URI string. The default setting of 'REQUEST_URI' works for most servers. | If your links do not seem to work, try one of the other delicious flavors: | | 'REQUEST_URI' Uses $_SERVER['REQUEST_URI'] | 'QUERY_STRING' Uses $_SERVER['QUERY_STRING'] | 'PATH_INFO' Uses $_SERVER['PATH_INFO'] | | WARNING: If you set this to 'PATH_INFO', URIs will always be URL-decoded! */ $config['uri_protocol'] = 'REQUEST_URI';
1.REQUEST_URI
REQUEST_URI模式是CI框架默认URL模式,采用分析$_SERVER[‘REQUEST_URI’]来区分控制器/动作/参数。例如:http://localhost/index.php?/user/login?var=value
$_SERVER[‘REQUEST_URI’]值:index.php?/user/login?var=value
实现代码如下:
$uri = parse_url('http://dummy'.$_SERVER['REQUEST_URI']); $query = isset($uri['query']) ? $uri['query'] : ''; $uri = isset($uri['path']) ? $uri['path'] : ''; if (isset($_SERVER['SCRIPT_NAME'][0])) { if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) { $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME'])); } elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) { $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); } } // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct // URI is found, and also fixes the QUERY_STRING server var and $_GET array. if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0) { $query = explode('?', $query, 2); $uri = $query[0]; $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : ''; } else { $_SERVER['QUERY_STRING'] = $query; } parse_str($_SERVER['QUERY_STRING'], $_GET);
在REQUEST_URI模式的基础上添加了重写规则的支持,可以去掉URL地址里面的入口文件index.php
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f ewriteRule ^(.*)$ index.php/$1 [L] </IfModule>
这样,就可以直接访问:http://localhost/user/login?var=value
2.QUERY_STRING
QUERT_STRING模式就是传统GET传参数来指定当前访问的模块和操作,例如: `http://localhost/?c=controller&m=method&var=value
c参数表示控制器,m参数表示控制器方法(这些参数都是可以配置的),后面的表示其他GET参数。
3.PATH_INFO
PATH-INFO:浏览器用GET方式发送数据时的附加路径
PATHINFO模式下的URL访问地址是: http://localhost/index.php/user/login/var/value/
Apache默认支持PATH_INFO, Nginx则需要改造一下:
location ~ ^(.+\.php)(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; }
PATHINFO安全漏洞:http://www.laruence.com/2010/05/20/1495.html (此漏洞已经修复,访问会出现Access denied)
TP
TP(3.2)框架中可以选择四种URL模式。
1.普通模式
普通模式也就是传统的GET传参方式来指定当前访问的模块和操作,例如: http://localhost/?m=home&c=user&a=login&var=value
m参数表示模块,c参数表示控制器,a参数表示操作(当然这些参数都是可以配置的),后面的表示其他GET参数。
如果默认的变量设置和你的应用变量有冲突的话,你需要重新设置系统配置,例如改成下面的:
'VAR_MODULE' => 'module', // 默认模块获取变量 'VAR_CONTROLLER' => 'controller', // 默认控制器获取变量 'VAR_ACTION' => 'action', // 默认操作获取变量
上面的访问地址则变成: http://localhost/?module=home&controller=user&action=login&var=value
2.PATHINFO模式
PATHINFO模式是TP3.2默认URL模式,PATHINFO模式下面的URL访问地址是: http://localhost/index.php/home/user/login/var/value/
PATHINFO地址的前三个参数分别表示模块/控制器/操作
3.REWRITE模式
REWRITE模式是在PATHINFO模式的基础上添加了重写规则的支持,可以去掉URL地址里面的入口文件index.php,但是需要额外配置WEB服务器的重写规则。
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [L] </IfModule>
接下来,就可以用下面的URL地址访问了: http://localhost/home/user/login/var/value
4.兼容模式
兼容模式是用于不支持PATHINFO的特殊环境,URL地址是: http://localhost/?s=/home/user/login/var/value
兼容模式配合Web服务器重写规则的定义,可以达到和REWRITE模式一样的URL效果。
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?s=/$1 [QSA,PT,L] </IfModule>
就可以和REWRITE模式一样访问下面的URL地址访问了: http://localhost/home/user/login/var/value
猜你喜欢
- 2024-10-11 含答案!2020年Nginx最牛面试题31问(一)拿大厂offer抱佛脚必备
- 2024-10-11 NginxWebUI - 图形化的 Nginx 配置管理工具
- 2024-10-11 Java一线互联网大厂高岗Nginx终极面试宝典,让你轻松KO面试
- 2024-10-11 Nginx之URL和目录不限制大小写限制
- 2024-10-11 教你分分钟搞定Linux下yum源配置(linux yum源配置命令)
- 2024-10-11 ASP.NET Core 中的 URL 重写中间件
- 2024-10-11 Nginx面试专题(nginx面试题汇总)
- 2024-10-11 一起来看看:大型电商网站详情页是如何支撑亿级流量访问的?
- 2024-09-21 Linux Nginx 进阶配置? nginx map
- 2024-09-21 高级开发必须掌握Nginx之三,server和 location配置
你 发表评论:
欢迎- 最近发表
-
- Linux新手必看:几种方法帮你查看CPU核心数量
- linux基础命令之lscpu命令(linux中ls命令的用法)
- Linux lscpu 命令使用详解(linux常用ls命令)
- 如何查询 Linux 中 CPU 的数量?这几个命令要知道!
- 在linux上怎么查看cpu信息(linux如何查看cpu信息)
- 查看 CPU 的命令和磁盘 IO 的命令
- 如何在CentOS7上改变网卡名(centos怎么改网卡名字)
- 网工必备Linux网络管理命令(网工必备linux网络管理命令是什么)
- Linux 网络命令知多少(linux 网络 命令)
- Linux通过命令行连接wifi的方式(linux命令行连接无线网)
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)