Nginx redirect path to a new structure not working with "#" - nginx

I'm trying to create the following rewrite in nginx:
https://my-domain/app/kibana#/discover?<some-get-params>
to
https://new-domain/app/discover#/?<some-get-params>
I'm struggling I think because of the "#" character.
I am able to redirect to the destination, but always without the params.
My last attempt was as follows:
location / {
rewrite ^/app/kibana#/discover(.*)$ https://my-new-domain.com/app/discover#/$1 redirect;
}
What happens with this one is a 404.
If instead I do something like:
location / {
rewrite ^/app/kibana(.*)$ https://my-new-domain.com/app/discover#/$args redirect;
}
I get redirected to the right destination but without any args: https://my-new-domain.com/app/discover#/
Same thing if instead of $args I use $1 instead

Related

Rewrite a rule in nginx so that every request that comes in goes to the root `/`

I want to write a rule in nginx.conf that will rewrite every incoming request (for eg. if its looking for a file under a sub path /subpath/index.js) to /index.js.
I tried the following block of code
`
check if request isn't static file
if ($request_filename !~* .(gif|html|jpe?g|png|json|ico|js|css|flv|swf|pdf|xml)$ ) {
rewrite (^[^?]+[^/?])([^/]*)$ $1/$2 permanent;
}
`

[nginx-config]: replace part of url using 'location'

How can I replace part of the URL using location to intercept the URL with the desired part?
here is nginx config part with location describing, but it is not working:
location /replace-before {
rewrite ^.*/replace-before(.*)$ $newPart$1 permanent;
break;
}
where $newPart = http://google.com
i have
http://localhost:4200/replace-before/data/user?user=name&place=place
and i need to replace 'replace-before' and all in front of it to $newPart,
The result should be like this:
http://google.com/data/user?user=name&place=place

Nginx URL rewrite for api/rest to rest/v1

Current API end points are like
domain.com/api/rest/[actual_module_routing_path, eg. cms/page/home]
In the new system this has to be something like
domain.com/rest/V1/$1
It has to work regardless of HTTP Method (GET, POST, PUT, DELETE). Trying something like this in nginx host config and I couldn't get it working, some help will be really appreciated
location /api/rest {
#Rewrite $uri=/api/rest/ back to just $uri=/rest/V1/$1
rewrite ^/api/rest/(.*)$ /rest/V1/$1 break;
}
location ~ /api/rest/(.*)
{
rewrite ^(.*)$ /rest/v1/$1 redirect;
}

Location redirect all /*.xml to /*

I want to redirect all /filename.xml files to /filename.
I can redirect an individual file like this
location = /mascus-export.xml {
rewrite .* /mascus-export redirect;
}
but rather than listing each .xml file like individually, I want redirect all. Something like this:
location = /*.xml {
rewrite .* /* redirect;
}
I've tried this type of thing in every combination
location = /(*)$.xml {
rewrite .* /$1 redirect;
}
but nothing seems to work.
You are trying to write a regular expression location block, which begins with ~ and not =. See this document for details.
For example:
location ~ \.xml$ { ... }
The rewrite needs to capture part of the URI using parentheses. For example:
rewrite ^(.*)\.xml$ $1 redirect;
The rewrite will work perfectly well either enclosed within the above location block, or just naked within the enclosing server block.
If you are going to use a regular expression location block, you do not need to use a rewrite statement too. Use a return statement instead. For example:
location ~ ^(.*)\.xml$ {
return 302 $1;
}

Nginx: filter characters from URI

I try to redirect some URLS with Nginx but having problems to combine filtering and stripping.
Redirect:
location ~* "^/7_skin/skin_archi/skin_control/text_box$" {
if ($request_uri ~* "([^/]*$)" ) {
set $last_path_component $1;
}
return 301 https://www.domain.com/wiki/display/$last_path_component;
}
this works fine and filters every path but last. Now I would like to strip characters (- and _) from $last_path_component but that doesn't seem to work.
I tried something like: rewrite ^(.*)[-_](.*)$ $1$2 permanent;
which worked, but not in conjunction with first filter as the whole path is given out.

Resources