user1310420
user1310420

Reputation:

nginx rewrite URL

I want to rewrite the following URL:

index.php?SOMETHING=VALUE

As

/SOMETHING/VALUE

I'm inexperienced with nginx rewrites so any help would be appreciated.

Thanks

Upvotes: 0

Views: 1133

Answers (1)

Mickaël Le Baillif
Mickaël Le Baillif

Reputation: 2157

I've come up with a solution to your problem :

location /index.php {
    if ( $args ~ "(?<PATH1>.*)=(?<PATH2>.*)" ) {
        rewrite ^ /${PATH1}/${PATH2}? last;
    }
}

Explanations:

  • if ( $args ~ "(?<PATH1>.*)=(?<PATH2>.*)" ) : captures the two relevant sections from the URL parameter, storing the values in variables PATH1 and PATH2
  • rewrite ^ means "rewrite the entire URI"
  • /${PATH1}/${PATH2} is constructing the new URI
  • the trailing ? informs nginx that you don't want to append the original URL parameters
  • last tells nginx to continue to follow rules after the rewrite

Upvotes: 1

Related Questions