Creating variable in apache config

穿精又带淫゛_ 提交于 2020-01-15 09:07:11

问题


I have an apache config something like below:

RewriteCond %{QUERY_STRING} ^site=(eu|jp|in)$ [NC]
RewriteRule ^/?fetchHomePage.action$ https://example.com/%1? [R=301,L,NC]

RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|jp|in)(?:&|$) [NC]
RewriteRule ^/?fetchFirstPage.action$ https://example.com/firstPage/%1? [R=301,L,NC]

RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|jp|in) [NC]
RewriteRule ^/?fetchSecondPage.action$ https://example.com/secondPage/%1? [R=301,L,NC]

Can I store "(eu|jp|in)" in some variable and reuse that variable in all the three rewrite rules?

Leads here are appreciated?


回答1:


(eu|jp|in) is actually a regex expression. You can't really store that part in a variable but you can use a env variable to avoid repetition of same conditions as this:

# set env var QS if quesry string has site=(eu|jp|in)
RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|jp|in)(?:&|$) [NC]
RewriteRule ^ - [E=QS:%1]

# now use value stored in QS variable
RewriteCond %{ENV:QS} ^(.+)$
RewriteRule ^/?fetchHomePage\.action$ https://example.com/%1? [R=301,L,NC]

RewriteCond %{ENV:QS} ^(.+)$
RewriteRule ^/?fetchFirstPage\.action$ https://example.com/firstPage/%1? [R=301,L,NC]

RewriteCond %{ENV:QS} ^(.+)$
RewriteRule ^/?fetchSecondPage\.action$ https://example.com/secondPage/%1? [R=301,L,NC]


来源:https://stackoverflow.com/questions/48821581/creating-variable-in-apache-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!