NGINX rewrite rule using two variables - nginx

I would like to achieve this but don't know how, or if it is possible:
web.com/anything => if web.com/show.php?query=anything
web.com/anything/something => if web.com/show.php?query=anything&query2=something
I mean , if there's only one variable it rewrites to web.com/variable
If there's a second variable, the first one turns into a folder now => web.com/variable/variable2

Just did it !!
Was more simple that I've imagined..
rewrite ^/([a-zA-Z]+)/([a-zA-Z]+)$ show.php?query=$1&query2=$2 last;

Related

Rewrite with Nginx then replace cars in results

I want /search/i-love-bacon to become /results?search_query=I+love+bacon.
actually I can get approxymative results with:
rewrite "^/search/(\w*)\-(\w*)" "/results?search_query=$1+$2" permanent;
But I don't know how many terms I can have at entry, it can be 1 or 10 and here actually I get only 2 terms.
Have to works in these cases:
/search/mom to /results?search_query=mom
/search/my-grandpa-is-old to /results?search_query=my+grandpa+is+old
/search/green-grass to /results?search_query=green+grass
/search/birds-and-elephants-can-swim to /results?search_query=birds+and+elephants+can+swim
You could recursively rewrite - to + before rewriting /search to /results. The recursive rewrite could be internal.
location / {
rewrite ^/search/(.*)-(.*)$ /search/$1+$2 last;
rewrite ^/search/(.*)$ /results?search_query=$1 permanent;
...
}
The recursive rewrite needs to be inside a location block (if it is internal, i.e. uses last). Either location / or location /search would do.
See this document for details.

nginx rewrite rules http://example.org/img?src=A to A?

My url format :
http://example.org/img?src=http://a.com/logo.png
I want nginx to rewrite this request to http://example.org/logo.png
(not url redriect)
how to do this?
The src argument is available as the $arg_src variable, however, you will need to use an if block to extract the part that you need. See this caution on using if.
For example (and you will need to adapt the regex to your specific needs):
location = /img {
if ($arg_src ~ \w(?<src>/\w.*)$) {
rewrite ^ $src last;
}
}
See nginx documentation here and a useful resource for regular expressions here.

cloudfront path pattern example for multiple subdomain origins

For example, a cloudfront distribution has the following CNAMEs associated with it
photo-cdn.example.com
video-cdn.example.com
music-cdn.example.com
And it has the following origins associated with it
originA: photo.example.com/photocontents
originB: video.example.com/videocontents
originC: music.example.com/musiccontents
I would like to be able to GET the following content files via the Cloudfront distribution as mapped below:
photo-cdn.example.com/photo.jpg => originA/photo.jpg
photo-cdn.example.com/image.png => originA/image.png
video-cdn.example.com/video.mpg => originB/video.mpg
video-cdn.example.com/movie.avi => originB/poster.jpg
music-cdn.example.com/music.mp3 => originC/music.mp3
music-cdn.example.com/itune.wav => originC/albumart.png
What should be the specific path patterns that i need to use to achieve this behavior?
Will the path pattern evaluation happen only on the SUFFIX of the distribution domain path (after music-cdn.example.com/)?
Or will the path pattern evaluation INCLUDE the distribution domain path (include music-cdn.example.com).
As long as you don't have overlapping file types you can use masks like /*.jpg, /*.png, /*.mpg etc.
But it won't work e.g. if you need to fetch .jpg album art from your music-cdn domain.
In this case you might need to split this into 3 different distributions, each having a single CNAME
EDIT: read the comments below for further clarification on which portions of the CNAME are actually evaluated by the path pattern evaluation

Rewrite /S123 to ?param=123

I have the following pattern for a htaccess-rewrite:
'^(?:.+/)?(\d+)/?$', 'index.php?page_id=61&id=$matches[1]'
which rewrites /123 to ?id=123
But I need /S123 rewritten.
I tried:
'^(?:.+/)?(\d+)/S?$', 'index.php?page_id=61&id=$matches[1]'
without luck.

Nginx rewrite rule with 2 variables

i need simple Nginx rewrite rule to rewrite
/?contidion1=variable1&condition2=variable2
to
/variable1/variable2/
This one doesn't work...
rewrite ^/condition/(.*)/([0-9])$ /?condition1=$1&condition2=$2 last;
Also tried:
rewrite ^/condition/(.*)/(.*)$ /?condition1=$1&condition2=$2 last;
However condition with 1 variable working good
rewrite ^/condition/(.*)$ /?condition1=$1 last;
You can use something like this or exactly this:
rewrite ^/\?condition1=([^&]*)&condition2=([^&]*) /$1/$2 last;
First rule in rewrite means what pattern your url must pass to be rewrited to secondrewrite rule. Next in second rule you can use parameter as $1 and $2 which correspondents to group in first rule (group is delimited by ( and ))
If doing in other way, because your suggestion what you want to do are a little different from your rewrite rules, do something like this:
rewrite /(.*)/(.*) /?condition1=$1&condition2=$2 last;

Resources