Nginx redirect - what am I doing wrong? - nginx

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.

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 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 rewrite rule exclude numbers

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;

and another Nginx redirect rewrite syntax q

To begin with, let me apologise for asking yet another question referring this topic.
I think I have read through all of them in the past few days, and still can not figure out a working solution for my needs.
Essentially I need nginx to redirect:
www.example.com/images/subfolders/.jpeg to images.example.com/subfolders/.jpeg
I currently have this setup:
location /images/ {
rewrite ^/(.*) http://images.example.com$request_uri? permanent;
}
And it kinda works, it redirects to the images.examle.com/images/*.jpeg , but what I need is it to skip the images folder, it will be much cleaner.
As well as that, has anybody seen some site with all of those symbols (^ ~ = + *)in nginx.cnfg explained ?
location /images {
rewrite ^/images(.*)$ http://images.example.com$1 permanent;
}
The explanation of the "^ ~ = + *" symbols can be found in the documentation of the location directive.

Resources