简体   繁体   中英

Apache mod_rewrite with variables

I need to modify my .htaccess in order to perfom the next redirect:

http://www.example.com/en/region1/subregion1/subregion2

to

http://www.example.com/en/example.php?param1=region1&param2=subregion1&param3=subregion2

The problem for me is that the original URL can have this 3 different structures:

/region1/

/region1/subregion1/

/region1/subregion1/subregion2

What I want is to convert this subdirectories in variables, whether they are 3 or only 1.

How can I achieve this with mod_rewrite?

Thank you

Take this as a base:

RewriteEngine on
RewriteRule ^en/(([^/]+)/)(([^/]+)/)?(([^/]+)/)?$ example.php?param1=$2&param2=$4&param3=$6

URL with only one param will be targeted to

example.php?param1=region1&param2=&param3=

So, non-existing params will be empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM