例子:
$url = "stackoverflow.com/index.php/section1/article22";
$myArray = array_slice( explode('/', $url), 2 );
echo "section: ". $myArray[0] ."<br /> article: ". $myArray[1]."<br />";
if(isset($myArray[0])) {
$section = $myArray[0];
} else {
$section = "";
}
if(isset($myArray[1])) {
$article = $myArray[1];
} else {
$article = "";
}
switch(strtolower($section)) {
default:
echo "home";
break;
case 'section1':
echo "function for find and show my article: ". $article;
break;
}
要使用保存在 db 中的部分,您可以使用 select 找到它的 id,然后是 Article
要将所有请求重定向到 index.php,请使用:
.htaccess
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond $0 !^(index\.php|css|js|img)
RewriteRule ^(.*)$ index.php [L]
索引.php:
$url = addslashes($_SERVER['REQUEST_URI']);
$myArray = array_slice( explode('/', $url), 1 );