I'm having trouble fine tuning a regex for Nginx url rewrite rules, What I am trying to do is take the first two pieces of the url and convert them to variables (nothing too fancy, and should be simple).
e.g. I type in http://www.webserver.com/piece1/piece2 and get http://www.webserver.com/rewtest.php??val1=piece1&val2=piece2
So far I have:
location / {
rewrite ^/(.*)/(.*)/? /rewtest.php?val1=$1&val2=$2 last;
return 404;}
}
which does seem to work. The problem is if the user types http://www.webserver.com/piece1/piece2/ it gives val 1 as piece1/piece2 (as 1 variable, not 2).
Also if the user were to type http://www.webserver.com/piece1/ I currently get piece1 in var 1, which is great. BUT if the user types http://www.webserver.com/piece1 it gives me an error and I'd like to get the same (var 1=piece1).
Any help greatly appreciated as I am new to regexs!
Seems as if / is also recognized by "."...try:
location / {
rewrite ^/([^/]+)/([^/]+)/? /rewtest.php?val1=$1&val2=$2 last;
return 404;}
}
Related
Those are my two nginx rules :
rewrite ^(/v1/foobar)(.*)$ /final$2 permanent;
rewrite ^(/v1/foobar-tow)(.*)$ /final$2 permanent;
I expect that /v1/foobar-two/foo will use the second rule and then will redirect to /final/foo but I doesn't : since the URL starts with foobar, the first rule will be used and finally will redirect to final-two/foo : the concatenation of the first target and the difference between the first and second rule !
To solve the problem I've just inversed the rules, but I wonder if there is a better solution !
To sum up, this is what I'm looking for :
/v1/foobar -> /final
/v1/foobar/hello -> /final/hello
/v1/foobar-tow -> /final
/v1/foobar-tow/hola -> /final/hola
To avoid surprises with NGINX, and increase configuration scalability, you should, generally:
try to live without rewrite directive; as per NGINX author it simply appeared before location and in many cases you can now use location with capture groups in lieu of rewrite
design your regular expressions in the way that the order of matching is not important, if possible
use exact matching, where applicable
isolate regular expression locations under a prefixed one
Putting all things together:
location /v1/ {
location = /v1/foobar {
return 301 /final;
}
location ~ ^/v1/foobar/(\w+)$ {
return 301 /final/$1;
}
location = /v1/foobar-tow {
return 301 /final;
}
location ~ ^/v1/foobar-tow/(\w+)$ {
return 301 /final/$1;
}
}
rewrite ^/#(.*)$ /user/$1/ last;
I want to rewrite foo.com/#bar to foo.com/user/bar.
Not an Nginx expert, but here is what you need to do
rewrite /#([^\W]+) /user/$1/ last;
to prove that, I have written a small Javascript code that demonstrates the replace part.
const url = "foo.com/#bar";
const result = url.replace(/#([^\W]+)/, "user/$1");
console.log(`the result is ${result}`);
Result
the result is foo.com/user/bar
Not an expert on regexs,
working on a nginx project, and we are looking for a way to map the first two (2) directories levels of a uri
map $uri $my_uri {
~^(?<base_uri>.*)/[^/]+$ $base_uri;
default $uri;
}
So far this code return the base_uri without the domain/server or any filenames
But I need this original request :
http://example.com/level1/level2/file.ext
http://example.com/level1/level2/level3/file.ext
http://example.com/level1/level2/level3/level4/[...more levels...]/file.ext
to return only the first 2 levels, regex should return
/level1/level2
Please note:
sub-folders (lavel1, lavel2) will be dynamic folder names, not static.
nginx's $url variable does not include protocol/server/port from the original request. so regex is performed against /level1/level2/level3/[...]/file.ext
any help will be appreciated.
I'm trying to get a param(h=1500 for example) via regex in a server block in an nginx server but it's not working. My last try was this:
location ~ "^/app/events/(?<eventid>\d+)/(?<image>.+)?h=(?<height>\d+)$" { ...... }
Here you can check and it works: https://regex101.com/r/kP9eY9/1
But in my server block file it does't.
If I try something like this, it works:
location ~ "^/app/events/(?<eventid>\d+)/(?<image>.+)/(?<height>\d+)$" { ...... }
Instead a param like "h=300", I just use a "/300" and I can get the value in my server block file.
I'm not a expert using regex so I can't see if there is something wrong. I need your help guys! Thank you!
From the documentation:
locations of all types test only a URI part of request line without
arguments
which means the ? and anything that follows it.
As #Richard mentioned, you can't use request arguments in locations regexps.
If you need to work with request arguments in your nginx config you might use $arg_ and/or $args syntax:
http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_
http://nginx.org/en/docs/http/ngx_http_core_module.html#var_args
I.e
location / {
if ($arg_param = 'someval') {
# some code here
}
}
I'm trying to pass a detail as a get variable using the Wordpress Rewrite Rule API. I'm not great with regex and it's driving me mad. I have tried every combination of the below and I'm not getting anywhere:
function dcc_rewrite_tags() {
add_rewrite_tag('%propref%', '([^&]+)');
}
add_action('init', 'dcc_rewrite_tags', 10, 0);
function dcc_rewrite_rules() {
add_rewrite_rule('^/[^/]*/([^/]*)/?', 'index.php?p=2&propref=$matches[1]', 'top');
}
add_action('init', 'dcc_rewrite_rules', 10, 0);
The url structure is mysite.com/page/get-variable/. I'm just trying to pass the last bit between the slashes as the get variable "propref". Most variations I've tried, it says it can't find the page or drops the last section of slashes.