在 apache 配置中创建变量
Creating variable in apache config
我有一个类似于下面的 apache 配置:
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]
我可以将“(eu|jp|in)”存储在某个变量中并在所有三个重写规则中重用该变量吗?
感谢这里的潜在客户?
(eu|jp|in)
实际上是一个正则表达式。你不能真正将该部分存储在变量中,但你可以使用环境变量来避免重复相同的条件:
# 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]
我有一个类似于下面的 apache 配置:
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]
我可以将“(eu|jp|in)”存储在某个变量中并在所有三个重写规则中重用该变量吗?
感谢这里的潜在客户?
(eu|jp|in)
实际上是一个正则表达式。你不能真正将该部分存储在变量中,但你可以使用环境变量来避免重复相同的条件:
# 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]