AnB
AnB

Reputation: 129

Nginx redirect to different location with part of the request header

I want to redirect certain requests to different location, but with part of the request header, example: https://example.com/something/value ---> https://example.com/something/index.php?var=value

pseudo-code:

location ^~ /something/$value {
    return 301 https://$host/something/index.php?var=$value;
}

Upvotes: 1

Views: 466

Answers (1)

Richard Smith
Richard Smith

Reputation: 49702

The location only needs to match that part of the URI which is constant. If /something/index.php and /something/value is the same prefix, then you do not want to use the ^~ modifier, otherwise the PHP file will not be found. See this document for details.

Use rewrite to capture the "value" part of the URI and append it as an argument.

For example:

location /something/ {
    rewrite ^/something/(.*)$ /something/index.php?var=$1 last;
}

If you want an external redirection use rewrite...permanent instead of rewrite...last. See this document for details.

Upvotes: 1

Related Questions