当前位置:Gxlcms > PHP教程 > php友好URL的实现(吐血推荐)_PHP

php友好URL的实现(吐血推荐)_PHP

时间:2021-07-01 10:21:17 帮助过:28人阅读

友好URL的实现(吐血推荐)
大家经常看到别的站的URL是这样的吧?
http://www.xxx.com/module/show/action/list/page/7
或者
http://xx.com/module/show/action/show/id/8.shtml 带扩展名的
或者
http://xx.com/module/show/action/show/id/8?word=ss&age=11
这样的吧
今天我就是公布下这种方法的实现,并独立出最简单的代码
函数如下,没封装成类,主要是没必要,用函数能方便些
代码如下:
  1. <br><!--?php <BR-->/** <br>* 获得友好的URL访问 <br>* <br>* @access public <br>* @return array <br>*/ <br>function getQueryString(){ <br>$_SGETS = explode("/",substr($_SERVER['PATH_INFO'],1)); <br>$_SLEN = count($_SGETS); <br>$_SGET = $_GET; <br>for($i=0;$i<$_SLEN;$i+=2){ <br>if(!empty($_SGETS[$i]) && !empty($_SGETS[$i+1])) $_SGET[$_SGETS[$i]]=$_SGETS[$i+1]; <br>} <br>$_SGET['m'] = !empty($_SGET['m']) && is_string($_SGET['m']) ? trim($_SGET['m']).'Action' : 'indexAction'; <br>$_SGET['a'] = !empty($_SGET['a']) && is_string($_SGET['a']) ? trim($_SGET['a']) : 'run'; <br>return $_SGET; <br>} <br>/** <br>* 生成链接URL <br>* <br>* @access public <br>* @param array $arr <br>* @return string <br>*/ <br>function setUrl($arr){ <br>global $Global; <br>$queryString=''; <br>if($Global['urlmode']==2){ <br>foreach($arr as $k=> $v){ <br>$queryString.=$k.'/'.$v.'/'; <br>} <br>} <br>$queryString.=$Global['urlsuffix']; <br>return $queryString; <br>} <br>?> <br> <br>使用很简单 <br><u></u> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->$_GET= getQueryString(); <br>?> <br> <br>但是这样还不行,这样只能实现 <br>http://www.xxx.com/index.php/module/show/action/list/page/7 这样的 <br>中间多了个index.php 为此我们要把他去掉,只好重写 <br>但是有些文件 又不希望这样,比如 样式 图片,那就放条件里 <br>建立一个 .htaccess文件 <br><u></u> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>RewriteEngine on <br>RewriteCond $1 !^(index\.php|css|pics|themes|js|robots\.txt) <br>RewriteRule ^(.*)$ index.php/$1 [L] <br> <br>现在OK了,赶快去测试吧 <br><u></u> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->$_GET= getQueryString(); <br>print_r($_GET); <br>?> </li></ol></pre></li></ol></pre></li></ol></pre>

人气教程排行