如何重写url,比如:
http://domain.com/class/method/parameter1/parameter2/.../parameterN
至
http://domain.com/index.php?c=class&m=method&p1=parameter1&...&pN=parameterN
其主要思想是创建使用无限数量的查询参数的可能性。
谢谢。
发布于 2010-04-27 18:57:23
使用Apache’s mod_rewrite module可以做到这一点:
RewriteRule ^/([^/]+/[^/]+)/([^/]+)(/.+)?$ /$1$3?p[]=$2 [N,QSA]
RewriteRule ^/([^/]+)/([^/]+)$ /index.php?c=$1&m=$2 [L,QSA]但是用PHP做这件事肯定会更容易:
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/'));
if (count($segments) >= 2) {
$_GET['class'] = array_shift($segments);
$_GET['m'] = array_shift($segments);
$_GET['p'] = $segments;
} else {
// error
}然后,您只需要一条规则来重写请求:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php$ index.php [L]https://stackoverflow.com/questions/2720460
复制相似问题