如果我在htaccess文件中使用了50次以下内容:
RewriteRule ^(bla | blo | bli | blu | bloi | bkdo | doid | boidi | woekj | dfpo8ds)/?$ / section_index.php?sectionurl = $ 1 [L]
然后可以将变量放入变量而不是在每一行中再次使用它吗?如果是这样,我该怎么做?
答案 0 :(得分:1)
您可以设置环境变量:
RewriteRule ^(bla|blo|bli|blu|bloi|bkdo|doid|boidi|woekj|dfpo8ds)/?$ /section_index.php?sectionurl=$1 [L,E=FOOBAR:$1]
现在可以使用%{ENV:FOOBAR}
访问该值。
修改另一种方法是按步骤处理请求并将规则链接起来:
# first path segment
RewriteRule ^(bla|blo|bli|blu|bloi|bkdo|doid|boidi|woekj|dfpo8ds)/?([^/].*)?$ $2?sectionurl=$1 [QSA,C]
# second path segment
RewriteRule ^(blog|foo)/?([^/].*)?$ $2?arg1=$1 [QSA,C]
# third path segment
RewriteRule ^(bar|baz)/?([^/].*)?$ $2?arg2=$1 [QSA,C]
# last rule
RewriteRule ^$ section_index.php [L,QSA]
但是当您使用PHP时,您还可以使用PHP来解析请求路径,例如:
$_SERVER['REQUEST_URI_PATH'] = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
$segments = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/'));
$argNames = array('sectionurl', 'arg1', 'arg2', 'arg3');
foreach ($segments as $i => $segment) {
if (isset($argNames[$i])) {
$_GET[$argNames[$i]] = $segment;
}
}
var_dump($_GET);
现在您只需要使用此规则将每个请求发送到该PHP文件:
# exclude requests for existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]