Home >Backend Development >PHP Tutorial >Implementation of php friendly URL (recommended by vomiting blood)_PHP tutorial

Implementation of php friendly URL (recommended by vomiting blood)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:49:47943browse

Implementation of friendly URLs (recommended by Hematemesis)
You often see the URLs of other sites looking like this, right?
http://www.xxx.com/module/show/action/list/page/7
or
http://xx.com/module/show/action/show/id/8 .shtml with extension
or
http://xx.com/module/show/action/show/id/8?word=ss&age=11
That’s it
That’s what I am today Announce the implementation of this method and separate the simplest code
The function is as follows. It is not encapsulated into a class, mainly because it is not necessary. It is more convenient to use functions

Copy the code The code is as follows:

/**
* Get friendly URL access
*
* @access public
* @return array
*/
function getQueryString(){
$_SGETS = explode(" /",substr($_SERVER['PATH_INFO'],1));
$_SLEN = count($_SGETS);
$_SGET = $_GET;
for($i=0;$i< ;$_SLEN;$i+=2){
if(!empty($_SGETS[$i]) && !empty($_SGETS[$i+1])) $_SGET[$_SGETS[$i]]= $_SGETS[$i+1];
}
$_SGET['m'] = !empty($_SGET['m']) && is_string($_SGET['m']) ? trim($ _SGET['m']).'Action' : 'indexAction';
$_SGET['a'] = !empty($_SGET['a']) && is_string($_SGET['a']) ? trim($_SGET['a']) : 'run';
return $_SGET;
}
/**
* Generate link URL
*
* @access public
* @param array $arr
* @return string
*/
function setUrl($arr){
global $Global;
$queryString='';
if($Global['urlmode']==2){
foreach($arr as $k=> $v){
$queryString.=$k.'/'.$v.'/';
}
}
$queryString.=$Global['urlsuffix'];
return $queryString;
}
?>

It’s easy to use
Copy the code The code is as follows:

$_GET= getQueryString();
?>

But this is not enough, this can only achieve
http://www.xxx .com/index.php/module/show/action/list/page/7 Such
has an extra index.php in the middle. For this reason, we have to remove it and have to rewrite
. But some files do not want it. In this way, for example, style pictures, then put
in the condition to create a .htaccess file
Copy the code The code is as follows:

RewriteEngine on
RewriteCond $1 !^(index.php|css|pics|themes|js|robots.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

Now it’s OK, go and test it quickly
Copy the code The code is as follows:

$_GET= getQueryString();
print_r($_GET);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319468.htmlTechArticle Implementation of friendly URLs (recommended for vomiting blood) You often see URLs like this on other sites, right? http://www.xxx.com/module/show/action/list/page/7 or http://xx.com/module/show/action/sh...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn