很多站长朋友们都不太清楚php配置url,今天小编就来给大家整理php配置url,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 ThinkPHP 网址格式URL地址怎么设置 2、 如何在PHP中实现URL路由 3、 phpcms怎么设置自定义url规则 4、 ThinkPHP 网址格式URL地址怎么设置~呢? ThinkPHP 网址格式URL地址怎么设置thinkPHP的URL在config中配置
一、URL规则
1、默认是区分大小写的
2、如果我们不想区分大小写可以改配置文件
'URL_CASE_INSENSITIVE'=>true,//url不区分大小写
3、如果模块名为 UserGroupAction,那么url找模块就必要要写成
4、如果'URL_CASE_INSENSITIVE'=>false,那么url也可以写为
二、URL伪静态
'URL_HTML_SUFFIX'=>'html|shtml|xml',//限制伪静态的后缀
三、URL路由
1、启动路由
要在配置文件中开启路由支持
'URL_ROUTER_ON'=>ture//开启路由
2、使用路由
1.规则表达式配置路由
'URL_ROUTE_RULES'=>array()//路由规则
'my'=>'Index/index',//静态地址路由
'my'=>'/Index/index',//静态地址路由,加/直接跳到网站根目录下。
':id/:num'=>'Index/index',//动态地址路由,可以$_GET接收地址栏参数
'year/:year/:month/:date'=>'Index/index',//动态和静态混合地址路由
'year/:year\d/:month\d/:date\d'=>'Index/index',//动态和静态混合地址路由加上 \d代表类型只能是数字
'my/:id$'=>'Index/index',// 加上$说明地址中只能是 my/1000 后面不能有其他内容了
2.正则表达式配置路由,必须以/开始 /结束
'/^year\/(\d{4})\/(\d{2})\/(\d{2})/'=>'Index/index?year=:1month=:2date=:3'//这里\d表示必须是数字
如何在PHP中实现URL路由第一步,首先要在服务器的配置上对/router/路径进行拦截
调用某个文件夹目录下的index.php页面,假定现在所有模块使用单独的文件存放于class目录下,该目录与router平级,如下图所示:
第二步,路由分发器的实现(index.php)
1: <!Doctype html>
2: <html>
3: <head>
4: <title>路由测试~~</title>
5: <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6: </head>
7: <body>
8:
9: <?php
10:
11: date_default_timezone_set("Asia/Shanghai");
12:
13: define("MODULE_DIR", "class/");
14:
15:
16: $_DocumentPath = $_SERVER['DOCUMENT_ROOT'];
17: $_FilePath = __FILE__;
18: $_RequestUri = $_SERVER['REQUEST_URI'];
19:
20: $_AppPath = str_replace($_DocumentPath, '', $_FilePath); //==>\router\index.php
21: $_UrlPath = $_RequestUri; //==>/router/hello/router/a/b/c/d/abc/index.html?id=3url=http:
22:
23: $_AppPathArr = explode(DIRECTORY_SEPARATOR, $_AppPath);
24:
25: /**
26: * ;url=http:
27: *
28: * /hello/router/a/b/c/d/abc/index.html?id=3url=http:
29: */
30:
31: for ($i = 0; $i < count($_AppPathArr); $i++) {
32: $p = $_AppPathArr[$i];
33: if ($p) {
34: $_UrlPath = preg_replace('/^\/'.$p.'\//', '/', $_UrlPath, 1);
35: }
36: }
37:
38: $_UrlPath = preg_replace('/^\//', '', $_UrlPath, 1);
39:
40: $_AppPathArr = explode("/", $_UrlPath);
41: $_AppPathArr_Count = count($_AppPathArr);
42:
43: $arr_url = array(
44: 'controller' => 'index',
45: 'method' => 'index',
46: 'parms' => array()
47: );
48:
49: $arr_url['controller'] = $_AppPathArr[0];
50: $arr_url['method'] = $_AppPathArr[1];
51:
52: if ($_AppPathArr_Count > 2 and $_AppPathArr_Count % 2 != 0) {
53: die('参数错误');
54: } else {
55: for ($i = 2; $i < $_AppPathArr_Count; $i += 2) {
56: $arr_temp_hash = array(strtolower($_AppPathArr[$i])=>$_AppPathArr[$i + 1]);
57: $arr_url['parms'] = array_merge($arr_url['parms'], $arr_temp_hash);
58: }
59: }
60:
61: $module_name = $arr_url['controller'];
62: $module_file = MODULE_DIR.$module_name.'.class.php';
63: $method_name = $arr_url['method'];
64:
65: if (file_exists($module_file)) {
66: include $module_file;
67:
68: $obj_module = new $module_name();
69:
70: if (!method_exists($obj_module, $method_name)) {
71: die("要调用的方法不存在");
72: } else {
73: if (is_callable(array($obj_module, $method_name))) {
74: $obj_module -> $method_name($module_name, $arr_url['parms']);
75:
76: $obj_module -> printResult();
77: }
78: }
79:
80: } else {
81: die("定义的模块不存在");
82: }
83:
84:
85: ?>
86:
87: </body>
88: </html>
获取请求的uri,然后拿到要加载的模块名、调用方法名,对uri参数进行简单的判断..
第三步,模块的编写
根据上述的uri,我们要调用的是Hello模块下的router方法,那么可以在class目录下定义一个名为Hello.class.php的文件(注意linux下是区分大小写的)
1: <?php
2:
3: class Hello {
4:
5: private $_name;
6: private $_varValue;
7:
8: function __construct() {
9:
10: }
11:
12: function router() {
13: $this->_name = func_get_arg(0);
14: $this->_varValue = func_get_arg(1);
15: }
16:
17: function printResult() {
18: echo $this->_name;
19: echo "<p>";
20: echo var_dump($this->_varValue);
21: echo "</p>";
22: }
23: }
24:
25: ?>
phpcms怎么设置自定义url规则phpcms自定义url规则,用到的程序:phpcms,步骤如下:
通过后台登录到phpcms管理后台,如下图:
2.点击扩展---url规则管理,根据自己的需要修改url规则。
说明:category是列表页和单页规则,show是内容页规则。
注意事项:根据seo需要链接一般三层。
ThinkPHP 网址格式URL地址怎么设置~呢?/ThinkPHP/conf/convention.php
/* URL设置 */
'URL_CASE_INSENSITIVE' => true, // 默认false 表示URL区分大小写 true则表示不区分大小写
'URL_MODEL' => 1, // URL访问模式,可选参数0、1、2、3,代表以下四种模式:
// 0 (普通模式); 1 (PATHINFO 模式); 2 (REWRITE 模式); 3 (兼容模式) 默认为PATHINFO 模式
'URL_PATHINFO_DEPR' => '/', // PATHINFO模式下,各参数之间的分割符号
'URL_REQUEST_URI' => 'REQUEST_URI', // 获取当前页面地址的系统变量 默认为REQUEST_URI
'URL_HTML_SUFFIX' => 'html', // URL伪静态后缀设置
'URL_DENY_SUFFIX' => 'ico|png|gif|jpg', // URL禁止访问的后缀设置
最好把这段复制到自己的application/commen/conf/config.php里修改。这样避免污染框架。
关于php配置url的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php配置url php配置apache的详细内容...