How to use RewriteMap

#1
Hello

I'm trying to replicate the configuration from Apache to Openlitespeed.
Every RewriteRule works fine but I can't replicate the RewriteMap directive on OLS.

Here a simple example

#################################
Configuration file vhost.conf
#################################
rewrite {
enable 1
autoLoadHtaccess 0
logLevel 9

map aree {
location txt:/usr/local/lsws/conf/vhosts/newsite/aree.txt
}
rules RewriteRule ^/?(it)/([^/?]+).html.*$ alloggi.php?lang=$1&aree=$\{aree:$2\} [L,NC]
}
#################################

#################################
Map file aree.txt
#################################
toscana 1
argentario 2
chianti 3
maremma 5
#################################

When I call
http://xxx.xxx.xxx.xxx/it/chianti.html
i see the following logs (logLevel 9)

2023-07-31 09:10:40.918909 [INFO] [33861] [xxx.xxx.xxx.xxx:yyyy#zzzzzzzzzzz] [REWRITE] Rule: Match '/it/chianti.html' with pattern '^/?(it)/(.*)\.html$', result: 3
2023-07-31 09:10:40.919270 [INFO] [33861] [xxx.xxx.xxx.xxx:yyyy#zzzzzzzzzzz] [REWRITE] Source URI: '/it/chianti.html' => Result URI: 'alloggi.php?lang=it&aree=${aree:chianti}'
2023-07-31 09:10:40.919391 [INFO] [33861] [xxx.xxx.xxx.xxx:yyyy#zzzzzzzzzzz] [REWRITE] replace current query string with 'lang=it&aree=${aree:chianti}'
2023-07-31 09:10:40.919503 [INFO] [33861] [xxx.xxx.xxx.xxx:yyyy#zzzzzzzzzzz] [REWRITE] Last Rule, stop!

The RewriteRule is found (result: 3) but I don't understand how to get the value in the areas.txt file...
the rule is replicated with
'lang=it&aree=${aree:chianti}'
when i would like to get
'lang=it&aree=3'

#################################
My configuration
#################################
Ubuntu 22.04.2 LTS
LiteSpeed/1.7.17 Open (BUILD built: Mon Jun 19 20:17:14 UTC 2023)
PHP 8.2
module versions
lsquic 3.2.0
modgzip 1.1
cache 1.64
mod_security 1.4
#################################

Thanks to anyone who can help me
 

metan

New Member
#3
Thanks for your test Cold-Egg!

I created the example "Product to ID configuration" as you suggested...but nothing!
(I'm not using the https protocol, which I see you are using).

I've tried with backslashes and without, using vhost file and .htaccess file configurations also.

- without backslashes:
Code:
RewriteMap product2id "txt:/usr/local/lsws/conf/vhosts/newsite/productmap.txt"
RewriteRule "^/product/(.*)" "/prods.php?id=${product2id:$1|NOTFOUND}" [PT]
I don't see any errors (not even one in error.log) and OLS give me 404 page

- with backslashes:
Code:
RewriteMap product2id "txt:/usr/local/lsws/conf/vhosts/newsite/productmap.txt"
RewriteRule "^/product/(.*)" "/prods.php?id=$\{product2id:$1|NOTFOUND\}" [PT]
OLS prints these logs
Code:
[REWRITE] Rule: Match '/product/stereo' with pattern '^/product/(.*)', result: 2
[REWRITE] Source URI: '/product/stereo' => Result URI: '/prods.php?id=${product2id:stereo|NOTFOUND}'
[REWRITE] replace current query string with 'id=${product2id:stereo|NOTFOUND}'
[REWRITE] Last Rule, stop!
and PHP print:
${product2id:stereo|NOTFOUND}

I think I have no more solutions
Cold-Egg, If you don't mind me asking, can you give me the exact code you used?

Thanks again
 

Attachments

Cold-Egg

Administrator
#4
Code:
rewrite  {
  enable                  1
  autoLoadHtaccess        1
  logLevel                9

  map product2id {
    location              txt:/var/www/html/productmap.txt
  }
RewriteRule "^/product/(.*)" "/prods.php?id=${product2id:$1|NOTFOUND}" [PT]
}
txt content
Code:
cat /var/www/html/productmap.txt
Product to ID map
##
## productmap.txt - Product to ID map file
##

television 993
stereo 198
fishingrod 043
basketball 418
telephone 328
test php file
Code:
cat /var/www/html/prods.php
<?php
echo $_GET['id'];


Log
[CODE][REWRITE] Rule: Match '/product/television' with pattern '^/product/(.*)', result: 2
[REWRITE] Source URI: '/product/television' => Result URI: '/prods.php?id=993'
[REWRITE] replace current query string with 'id=993'
[REWRITE] Last Rule, stop!
Hove the above information helps.
 

metan

New Member
#5
Cold-Egg you saved my upgrade to OLS!

The problem was in the permissions of file "productmap.txt"

setting lsadm:nogroup with rwxr-x--- permissions

Everything works great :cool:(y)

Thank you very much!
 
Top