Nginx redirect rule with arabic characters - nginx

I need to create an Nginx rule that redirects /ae/ar/أسافر%20من%20أجل/ to /ae/en. I've tried with these two rules:
rewrite (?i)^/ae/ar/أسافر%20من%20أجل(.*)$ http://$host/ae/en? permanent;
rewrite (?i)^/ae/ar/%D8%A3%D8%B3%D8%A7%D9%81%D8%B1%20%D9%85%D9%86%20%D8%A3%D8%AC%D9%84(.*)$ https://$host/ae/en? permanent;
But no one worked. Looks like the rule is ignored, any clues to make this rule work properly?
Thanks in advance!

Rewrite does not see the percent encoded characters. You should be able to leave the arabic characters as they are (UTF-8 characters within the configuration file) and replace the %20 with a single space. The entire regular expression should be surrounded with "s.
Alternatively, use a location statement:
location = "/ae/ar/أسافر من أجل/" { return 301 /ae/en; }

Related

nginx rewrite regex matching first group instead of all

I'm working on an nginx rewrite rule to redirect:
/collections/collection-name/products/product-handle-with-dashes
to:
/products/product-handle-with-dashes
I've got it almost working, the only issue I have right now if my rule to match the product handle is only returning the first string before the first hyphen.
My rule:
rewrite ^(/collections/.*)/products/(\w+)\.?.*$ /products/$2 permanent;
With this rule if I hit the following path: /collections/collection-name/products/some-product-handle it will redirect me to /products/some
what am I missing on my regex to allow it my second variable to capture the entire handle with dashes.
\w metacharacter of the PCRE/PCRE2 regex patterns include characters from ranges a-z, A-Z, 0-9 and underscore. It does not include a hyphen. You probably want [-\w] instead. The whole rewrite rule can be the
rewrite ^/collections/[^/]+(/products/[-\w]+)(\.\w+)?$ $1 permanent;

nginx escaping (+) sign in rewrite rule

I am trying to redirect some hits based on the image name in my Nginx configuration.
This is how it looks now:
location / {
rewrite ^/static-v3/(.*)/creditcard_sslseals_public.png https://somenewurl.com/credit-card-seals.png permanent;
rewrite ^/static-v3/(.*)/creditcard+sslseals_public.png https://somenewurl.com/credit-card-seals.png permanent;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
So the first condition with the filename creditcard_sslseals_public.png works properly, but the second is not working since there is a + sign in the image name like creditcard+sslseals_public.png, so I am getting a 404.
How can I escape the + in the second condition, but keep the regex before ^/static-v3/(.*)?
The + has a special meaning in a regular expression and needs to be escaped.
One option is to use a backslash to escape the character:
^/static-v3/(.*)/creditcard\+sslseals_public.png
Alternatively, create a character class containing just one character:
^/static-v3/(.*)/creditcard[+]sslseals_public.png

nginx location match rule, without matching trailing characters

I have the following rule:
location /foo {
Which matches well for the following examples:
mydomain.com/foo
mydomain.com/foo/
mydomain.com/foo/bar?example=true
However it is also matching for
mydomain.com/foobar
I don't want it to match to that last one (/foobar), it should only match if there is either nothing after the foo, or a slash and zero or more characters after it. I've tried location /foo/ { but that does not produce desired results either.
Can anyone shed some light on how to do this?
There are two ways to handle this, use a regular expression location block - or just handle /foo separately from /foo/.
Regular expression location blocks have a different evaluation order and are less efficient than prefix location blocks, so my preferred solution is the exact match location and prefix location.
Generally, /foo just redirects to /foo/, for example:
location = /foo {
return 302 /foo/;
}
location /foo/ {
...
}
See this document for more.
You can create one Location rule to /foobar whether you need it as an exception:
location = /foobar {
....
}
Nginx match first URI by = operator.

Nginx rewrite with and without trailing slash

I'm trying to create a set of rules that match a url with and without a trailing slash
Most of the answers were pointing me to use something similar to this.
location /node/file/ {
rewrite ^/node/file/(.*)/(.*)$ /php/node-file.php?file=$1&name=$2;
rewrite ^/node/file/(.*)/(.*)/?$ /php/node-file.php?file=$1&name=$2; │
}
But this does not match the trailing slash url.
How can I write a rule that matches urls that look like
http://example.com/node/file/abcd/1234/
http://example.com/node/file/abcd/1234
The first rewrite statement includes (.*) as the last capture, which will match any string, including one with a trailing slash.
Use the character class [^/] to match any character except the /:
rewrite ^/node/file/([^/]*)/([^/]*)$ /php/node-file.php?file=$1&name=$2;
rewrite ^/node/file/([^/]*)/([^/]*)/?$ /php/node-file.php?file=$1&name=$2;
Now you will notice that the first rewrite statement is unnecessary, as the second rewrite statement matches URIs both with and without a trailing /.
So all you need is:
rewrite ^/node/file/([^/]*)/([^/]*)/?$ /php/node-file.php?file=$1&name=$2;

Nginx Regular expression to match first segment of a url

I have some urls like these:
domain.com/article/1234-newstitle
domain.com/article/1234-newstitle/
domain.com/article/1234-newstitle/abc
domain.com/article/1234-newstitle/xpto/abc
domain.com/article/1234-newstitle/qwerty/abc/xyz
I want to catch only the /1234-newstitle/ to redirect these urls and ignore everything after the 1º slash (whatever the number of segments) so I can have:
domain.com/news/newstitle-1234/
The best I could get is:
rewrite ^article/(\d+)-(.*)[^\/]* /new/$2-$1/ permanent;
but $2 matches everything after 1234-
How am I able to match only the first segment "1234-newstitle" and ignore the rest?
Looks like you've got a greedy match after the hyphen. Try this:
rewrite ^/article/(\d+)-([^/]+) /new/$2-$1/ permanent;

Resources