tags:

views:

40

answers:

1

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

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?

+1  A: 

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}.


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:

$_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:

# exclude requests for existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Gumbo
That doesn't seem to work, this is what I'm trying: RewriteRule ^(%{ENV:FOOBAR})/?$ /section_index.php?sectionurl=$1 [L]
You cannot use variables in the search pattern. But you could use an additional `RewriteCond` like as `RewriteCond %{ENV:FOOBAR}%{REQUEST_URI} ^(bla|blo|bli|blu|bloi|bkdo|doid|boidi|woekj|dfpo8ds)/\1`. But that’s not quite elegant.
Gumbo