问题
I want to make my own url rewriting rules using couple of PHP-MySQL. The concept is to have a rule in my .htaccess that send all request to my index.php file like :
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php
RewriteCond %{REQUEST_URI} !\.(.+)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301].
And php will take all params via a super global var like $_SERVER['REQUEST_URI'] and explode("/", $_SERVER['REQUEST_URI']).
function rewrite(){
$paramKeys = array("", "locale", "page", "par1", "par2", "par3", "par4", "par5");
$paramValues = explode("/", $_SERVER['REQUEST_URI']);
foreach($paramValues as $key => $value){
if (!is_array($key)) {
$paramValues[$key] = htmlspecialchars(stripslashes(trim($value)));
if($key == 0){ //but the 1st slash is in the end of URL
continue;
}
elseif($key == sizeof($paramKeys)){
break;
}
else $params[$paramKeys[$key]] = $value;
}
else continue;
}
return $params;
}
And compare the requested URL with URL's in my database to send http statut in order of the requested file is found 200, moved 301, or not found 404.
Have I a bad idea ? if not, how can I perfectionize it. Thank you !
来源:https://stackoverflow.com/questions/13646561/use-php-to-rewrite-urls