mod_rewrite如果你在整个地方使用一个表达式,你能把它放在一个“变量”中吗?

时间:2022-05-16 11:20:35

If I use the following like 50 times in my htaccess file:

如果我在htaccess文件中使用以下50次:

RewriteRule ^(bla|blo|bli|blu|bloi|bkdo|doid|boidi|woekj|dfpo8ds)/?$ /section_index.php?sectionurl=$1 [L]

RewriteRule ^(bla | blo | bli | blu | bloi | bkdo | doid | boidi | woekj | dfpo8ds)/?$ / section_index.php?sectionurl = $ 1 [L]

is it then possible to just put bla|blo|bli|blu|bloi|bkdo|doid|boidi|woekj|dfpo8ds in a variable instead of using it again in every line? If so, how do I do that?

那么有可能只在一个变量中放入bla | bl | bli | blu | bloi | bkdo | doid | boidi | woekj | dfpo8ds而不是在每一行中再次使用它吗?如果是这样,我该怎么做?

1 个解决方案

#1


You could set an environment variable:

您可以设置一个环境变量:

RewriteRule ^(bla|blo|bli|blu|bloi|bkdo|doid|boidi|woekj|dfpo8ds)/?$ /section_index.php?sectionurl=$1 [L,E=FOOBAR:$1]

The value is now accessible with %{ENV:FOOBAR}.

现在可以使用%{ENV:FOOBAR}访问该值。


Edit    Another way would be to process the request in steps and chain the rules:

编辑另一种方法是按步骤处理请求并链接规则:

# 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]

But as you use PHP, you could also use PHP to parse the request path, for example:

但是当您使用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);

Now you just need to send every request to that PHP file by using this rule:

现在,您只需使用此规则将每个请求发送到该PHP文件:

# exclude requests for existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

#1


You could set an environment variable:

您可以设置一个环境变量:

RewriteRule ^(bla|blo|bli|blu|bloi|bkdo|doid|boidi|woekj|dfpo8ds)/?$ /section_index.php?sectionurl=$1 [L,E=FOOBAR:$1]

The value is now accessible with %{ENV:FOOBAR}.

现在可以使用%{ENV:FOOBAR}访问该值。


Edit    Another way would be to process the request in steps and chain the rules:

编辑另一种方法是按步骤处理请求并链接规则:

# 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]

But as you use PHP, you could also use PHP to parse the request path, for example:

但是当您使用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);

Now you just need to send every request to that PHP file by using this rule:

现在,您只需使用此规则将每个请求发送到该PHP文件:

# exclude requests for existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]