Mod_rewrite, if you use expression all over the place, can you put it in a "variable"?
If I use the following 50 times in my htaccess file:
RewriteRule ^ (bla | blo | bli | blu | bloi | bkdo | doid | boidi | woekj | dfpo8ds) /? $ / section_index.php? sectionurl = $ 1 [L]
can we just put bla | blo | bli | blu | bloi | bkdo | doid | boidi | woekj | dfpo8ds to a variable instead of using it again on every line? If so, how do you do it?
0
source to share
1 answer
You can 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 available with %{ENV:FOOBAR}
.
Edit . Another way is to process the request step by step and link 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 since you are using PHP, you can also use PHP to parse the request path, for example:
$_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 this PHP file using this rule:
# exclude requests for existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
+1
source to share