301 Only Part of the URL in Nginx - regex

Trying to 301 a url by matching just its post id. For example I just want to rewrite any url that ends in -158436, yet I can't seem to make it work out.
location /forum/this-stuff-does-not-matter-158436 {
rewrite ^ http://www.thegoodurl.com/whatever/ permanent;
}

Related

Nginx Redirect based on what is contained in the url

I am a noob when it comes to redirect and rewrites and such... My goal is to do a redirect based on what is sent to the server.
Example of the urls that will be sent:
https://example.org/git/#123
https://example.org/git/!123
and redirect to url based if ! or # is part of it
if ! then it would go to
https://git.example2.com/userhere/repohere/merge_requests/123
if # then it would go to
https://git.example2.com/userhere/repohere/issues/123
Some help with a nginx location block would be nice.
You can’t match #123. So you need to use different character i.e &
Use below sample rule.
if ($request_uri ~ "^(.*)/!123)"{
return 301 https://git.example2.com/userhere/repohere/merge_requests/123;
}
if ($request_uri ~ "^(.*)/!456)"{
return 301 https://git.example2.com/userhere/repohere/merge_requests/456;
}

Nginx rewrite rule to redirect from requested url to specific url

I'm trying to redirect from requested html file to specific location.
for example:
FROM
https://example.com/cars/description/ferrari-1-1532.html
to
https://example.com/car/catalog/ferrari.html
I tried this configuration
location /cars/description/*.html {
rewrite ^(.*)$ https://examples.com/car/catalog/request_filename redirect;
}
But it is not working.
This location should redirect all requests to html files in /cars/description directory to this url https://examples.com/car/catalog/request_filename
location ~* /cars/description/(.+\.(html))$ {
rewrite ^(.*)$ https://examples.com/car/catalog/request_filename redirect;
}

Nginx 301 redirect a url with query string

I have page with pagination which google marked as duplicate content, basically I have this page /asl/candidates/ and /asl/candidates/?page=1 is the same page. What I want to do is to 301 redirect /asl/candidates/?page=1 to /asl/candidates/
I tried
location = /asl/candidates/?page=1{
return 301 /asl/candidates/;
}
But it seems it does not work with query strings.
Use $args or $arg_name if you need to get the argument(s) in the request line.
For example:
if ($args ~ "page=1") {
rewrite ^/asl/candidates/$ /asl/candidates/? permanent;
}
Source: http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

Nginx redirect based on referring URL regular expressions

New to nginx and still trying to figure out its methods.
I'm trying to do a redirect to an external URL based on the referring URL. For example, in the code below that I have for the hosted domain, if the referring URL comes from Facebook, I want to redirect the user to a specific URL:
location / {
index index.php;
if ($http_referer ~* ^(.*?(\bfacebook\b)[^$]*)$ ) {
rewrite http://www.othersite.com break;
}
try_files $uri $uri/ #handler;
expires 30d;
}
Nginx doesn't throw any errors once it's restarted, but despite testing this from a Facebook link, it's not executing.
Any nginx / regular expression gurus who can point me in the right direction?
Thanks in advance.
Although it may pass the syntax test, your rewrite statement is incorrect. To redirect any URI to a new URL you would use:
rewrite ^ http://www.example.com/? permanent;
But the preferred solution would be the more efficient:
return 301 http://www.example.com/;
See this page for details of both directives.

nginx rewrite url only with certain subdomain

I am trying to make my nginx rewriting all the urls with certain subdomain (m.example.com) to add string: ?theme=XXX to the end of the file
In my nignx server configuration i have:
server{
server_name example.com m.example.com y.example.com
(...)
}
Now, I want to rewrite all http requests going ONLY through m.example.com to add to the end of the url ?theme=XXX
I have the following rewrite:
rewrite ^(.*)$ $1?theme=XXX? break;
but i don't know how to make rewriting only requests from m.example.com (not from all server names)