In Nginx, how to match and entire URL and Query String and redirect to a URL and Query String
Ad
I have been searching for a while now, trying work arounds and haven't come up with anything useful.
I have a (large) list of URL's from an site migration and need to match the entire URL + Query String and redirect to another URL.
As far as I can see the following only matches /mens
, but not the rest of the query string.
rewrite "^/mens?brand%5B%5D=27§ion%5B%5D=5&price-min=0&price-max=2000&sort=newest" "/t/gender/men" permanent;
The reason it's important is that I have a bunch of similar URL's with slightly different Query Strings, which need to be redirected, similar to below, but actually work.... :-/
rewrite "^/mens/shop?q=road+map+polo" "/t/category/golf-knits" permanent;
rewrite "^/mens/shop?q=six+pocket+pant" "/t/category/golf-pants" permanent;
#etc... ad noiseam
Thanks in advance, Paul.
Ad
Answer
Ad
The $request_uri
variable contains the entire URL. You could use a map
to translate it into a redirection.
map $request_uri $target {
~*^/mens/shop\?q=road\+map\+polo /t/category/golf-knits;
~*^/mens/shop\?q=six\+pocket\+pant /t/category/golf-pants;
}
server {
...
if ($target) { return 301 $target; }
...
}
See this document for details.
Ad
source: stackoverflow.com
Related Questions
Ad
- → How to replace *( in a string
- → Regex extract string after the second "." dot character at the end of a string
- → CodeMirror regex doesn't match behavior typical Javascript regex parser
- → JS Get multiple links in a string
- → Using capture group $1 to access object's key value
- → Difference between /(a|b)/ and /[ab]/ in JavaScript split() using regex
- → In Nginx, how to match and entire URL and Query String and redirect to a URL and Query String
- → Reorder lines in a string based on first two numbers inside
- → How to manage URL with or without ? and /
- → Not sure why this Regex is returning true
- → Laravel extract excerpt from content using tinymce
- → Cannot get my multiple regex working for specific case in URL structure
- → laravel5.1 validate number
Ad