nginx rewrite rule exclude numbers - nginx

Need some help with Nginx write rule such that:
All urls of type:-
/1.1.1/xyz/abc/z.js
/2.2.2/xyz/def/c.css
are re-directed to
/xyz/abc/z.js
/xyz/def/c.css
Want to exclude the numeric part which comes at the beginning of URL.

location ~ ^/[0-9\.]+(/.*) {
try_files $1 $1/;
}

this will work
rewrite ^/(d+).(d+).(d+)/(.+)$ /$1;

Related

Rewrite URLs with different domains and similar paths

I need to redirect all URLs formatted as http://www.example.org/dir/subdir/ to http://subdomain.example.org/subdir/. For example, http://www.example.org/dir/subdir/page would redirect to http://subdomain.example.org/subdir/page.
I tried rewrite ^(/dir/subdir.*) http://subdomain.example.org$1 permanent;, but this keeps the /dir/ part, which I want to omit.
Your rewrite rule is capturing the /dir/ part too.
Try this:
location /dir/subdir {
rewrite ^/dir(.*)$ http://subdomain.example.org$1 permanent;
}

Nginx Remove Trailing Question Mark

I'm trying to make a rewrite rule in Nginx to remove trialing question mark (?) from urls but i can't get it right. I've done that for trailing slashes like this:
#redirect all trailing slash URL's to the respective non trailing slash
rewrite ^/(.*)/$ /$1 permanent;
so I figured the same would work just replacing the slash with the question mark:
rewrite ^/(.*)?$ /$1 permanent;
but that didn't work, but it occurred to me that the question mark has some significance in the regex so i tried escaping it:
rewrite ^/(.*)\?$ /$1 permanent;
but that didn't work either, I tried also removing the first slash:
rewrite ^(.*)\?$ $1 permanent;
but that was also a bust, and yes i did restart the server in between tests.
Here's what I am trying to do:
www.mysite.com? should redirect to wwww.mysite.com
www.mysite.com/some/path? should redirect to wwww.mysite.com/some/path
www.mysite.com?some=vars should remain unchanged.
www.mysite.com/some/path?some=vars should remain unchanged.
so basically only removing the question mark if there is no query string.
How can i accomplish that?
I've checked other answers but they seem to want to remove the query string entirely, I only want to remove in the case that there is only a question mark and no parameters.
The ? marks the start of the query string and is not part of the normalized URI used by the rewrite or location directives. So you cannot remove it using a rewrite statement.
You will need to look at the original request which is in the $request_uri variable.
For example:
if ($request_uri ~ ^(.*)\?$) { return 301 $1; }
See this caution on the use of if.

Nginx location group capture + rewrite

I'm trying to do a group capture in a Nginx location block and it's not working for me.
Is what I am trying to do even possible?
location ~* /(?<cat>cars|trucks|bikes|motorcycle|quads) {
rewrite ^/$cat/([0-9]+)(.*)$ /page.php?id=$1 last;
}
The error message I am receiving is :
"^/$cat/([0-9]+)(.*)$" does not match "/cars/120/new-car-rentals/"
I have a lot more categories than what I am posting, and trying to prevent writing a rewrite 5x for each specific category name.
Any help would be appreciated.
I'm not familiar with this particular syntax, but based on my experience with others, is it possible that you simply need to escape the forward slashes you're using?
location ~* \/(?<cat>cars|trucks|bikes|motorcycle|quads) {
rewrite ^\/$cat\/([0-9]+)(.*)$ /page.php?id=$1 last;
}
Note the named capture in the location regex: if you want to use a value captured here, you must use the named syntax (?<name>), numbers do not work.
I solved the issue by doing this instead :
location ~* /(cars|trucks|bikes|motorcycle|quad-bikes) {
rewrite ^/([a-zA-Z-]+)/([0-9]+)(.*)$ /page.php?id=$2 last;
...
...
}
The regex ([a-zA-Z-]+) allows me to use characters a-z (case insensitive) with possible dashes in my category / page names.

Nginx rewrite individual urls with get parameters to pretty urls

I have categories I'd like to rewrite.
for example:
example.com/videos?c=18
to:
example.com/category/name
I tried to do this using multiple examples, most had no effect and this example gave me only page not found to all /videos pages:
location /videos {
if ($args ~ "c=18") {
rewrite ^/videos(.*) http://$server_name/category/name$1 permanent;
}
}
Is this even doable purely via Nginx what I am trying to achieve?
As far as I got the question, currently your site has this url scheme, which you can't change:
http://example.com/videos?c=18
But you would like to present visitors with "pretty"-looking URLs like
http://example.com/category/name
That pretty URL does not really exist anywhere on the site, which is why you have to rewrite it, e.g. turn pretty virtual url into a real one that your scripts can process.
Once again, you rewrite from virtual to actual, not the other way round.
The following directive would turn /category/cars/ into /videos?c=cars
location /category {
rewrite ^/category/(.*)$ /videos?c=$1 last;
}
But your script won't understand /videos?c=cars url, it needs category ID to work. So in your case the pretty url should look like
http://example.com/category/18
which will be rewritten to
http://example.com/videos?c=18
"if" directive is not best solution, but in your case you can try "if" and $arg_name (argument name in the request line):
location /videos {
if ($arg_c = "18") {
rewrite ^/videos(.*) http://$server_name/category/cars? permanent;
}
if ($arg_c = "19") {
rewrite ^/videos(.*) http://$server_name/category/bikes? permanent;
}
# and so on
}

Nginx redirect - what am I doing wrong?

The original url is https://www.mywebsite.com/women/shoes_1-+-1~2.html
The redirected url should be https://www.mywebsite.com/women/shoes.html
rewrite ^/women/shoes_1-+-1~2\.html /women/shoes.html permanent;
I am sure the answer is simple but I don`t see it!
Thank you for your help.
The problem was that I did not put "\" before the "-" and "+".
The final version that I used is:
rewrite ^/women/shoes_1\-\+\-1~2\.html /women/shoes.html permanent;
I would do it like this:
location ~ ^/women/shoes_1\-\+1~2\.html$ {
rewrite ^(.*)$ /women/shoes.html permanent;
}
For the best performance, I would try:
location = /women/shoes_1-+-1~2.html {
return 301 https://www.mywebsite.com/women/shoes.html;
}
Note that rewrite directives and regex locations are dependent on their position in your config. By avoiding them, your config will scale more smoothly.

Resources