Replace key-value pair in query string with Apache2

时间:2015-11-12 10:42:20

标签: mod-rewrite apache2

I am trying to set a simple replacement in my URL query strings.

I have an absolute path in my query string I would like to be replaced by a custom string.

FROM  http://acme.com/a/path?file=DIR/this.file&foo=2
  TO  http://acme.com/a/path?file=/long/absolute/path/to/this.file&foo=2

These are my directives:

# [sudo a2enmod rewrite]
RewriteCond %{QUERY_STRING} ^(.*)file=DIR(.*)$
RewriteRule ^/a/path /a/path?%1file=/long/absolute/path/to%2

With such a configuration, replacement does not work, DIR passes through. I configured a LogLevel to trace1 in my Apache2 configuration but I get nothing in my access.log and no useful feedback in error.log.

The question is:


References:

1 个答案:

答案 0 :(得分:1)

这对我有用(我已经添加了标志R和L):

RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)file=DIR(.*)$
RewriteRule ^/a/path /a/path?%1file=/long/absolute/path/to%2 [R,L]

另见https://httpd.apache.org/docs/2.4/rewrite/flags.html