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;
Related
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.
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.
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.
I'm just moving my blog over to Ghost and all was ok except for around 50% of my blog posts are broken due to the date not being zero padded i.e.
old site format:
http://www.example.com/blog/index.cfm/2013/8/9/my-slug
new site format:
http://www.example.com/2013/08/09/my-slug
Removing the /blog/index.cfm was easy via
location /blog/index.cfm {
rewrite ^/blog/index.cfm(/.*)$ $1 last;
}
But cannot think of a way to zero pad dates (and there is around 700 posts).
Put several rewrites.
location /blog/index.cfm {
# 2013/1/1
rewrite ^/blog/index.cfm(/\d+)/(\d)/(\d)(/.*)?$ $1/0$2/0$3$4 last;
# 2013/1/11
rewrite ^/blog/index.cfm(/\d+)/(\d)/(\d\d)(/.*)?$ $1/0$2/$3$4 last;
# 2013/11/1
rewrite ^/blog/index.cfm(/\d+)/(\d\d)/(\d)(/.*)?$ $1/$2/0$3$4 last;
# all other
rewrite ^/blog/index.cfm(/.*)$ $1 last;
}
Hi need help to reqrite this rule from apache to nginx, trying all day but nothing:
apache:
^pimage/small-([^/.]+)-([^/.]+).jpg$ /img_on_fly.php?iname=$1&iuid=$2&isize=small
tried with all online converters, tried everything I found online and nothing. I have no experience with nginx but other 15 rules rewrited one by one, but all were simple than this, so if someone can. Also, I am not sure what is "location" for this rule in nginx conf?
try the following in the server element of your conf file
location ~ ^\/pimage\/small\-(?<iname>[^\/\.]+)\-(?<iuid>[^\/\.]+)\.jpg$ {
try_files $uri /img_on_fly.php?iname=$iname&iuid=$iuid&isize=small
}
your conf file should look like:
server {
...
...
...
}
you can just change it to
server {
...
...
...
location ~ ^\/pimage\/small\-(?<iname>[^\/\.]+)\-(?<iuid>[^\/\.]+)\.jpg$ {
try_files $uri /img_on_fly.php?iname=$iname&iuid=$iuid&isize=small
}
}
What about escaping the dots, like so:
rewrite ^pimage/small-([^/\.]+)-([^/\.]+).jpg$ /img_on_fly.php?iname=$1&iuid=$2&isize=small;
try it and comment back.
UPDATE:
Try escaping the dashes too, like so:
rewrite ^pimage/small\-([^/\.]+)\-([^/\.]+).jpg$ /img_on_fly.php?iname=$1&iuid=$2&isize=small;
After try that, try also the variation of "dots not escaped and dashes escaped".