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
Related
I am trying to build a rewrite for CDN system which uses arguments to point to the data. All data is stored in the same location so any rewrite needs to wildcard rewrite the $arg_name and then pass the value for the data along. Turning this:
http://cdn.example.com/data/?main_loc=datastring
into this:
http://example.com/datastore/datastring
Using a location directive I can fetch the request to /data/, but from there I don't know what the rewrite would need to look like to do a "match any arg name" like main_loc, backup_loc etc. to pass its value as rewrite.
Can I apply regex to match any $arg_name and use the value from that?
location ^~ /data/ {
rewrite ^$arg_(\w*\_\w*)$ http://example.com/datastore/$1;
}
Or what would that look like?
You could capture the arg value with the following map:
map $args $arg_value {
~=(.*) $1;
}
Then redirect to it:
location = /data/ {
return 301 http://example.com/datastore/$arg_value;
}
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
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;
}
My URL looks like:
localhost/video-detail?videoID=T0r-uCXvDzQ
I want to serve a page with name: T0r-uCXvDzQ.html (videoID.html) which is present in server's file system.
I am trying to write the rewrite rule as follows:-
location / {
rewrite ^/video-detail?videoID=(.*) /$1.html;
}
Also tried:
location / {
rewrite ^/video-detail?videoID=(.*) /$arg_videoID.html;
}
But they are giving 404 error.
How can I use the query parameters in the output rewrite rule.
The following worked for me:-
if ($args ~* "videoID=(.*)") {
set $key1 $1;
rewrite ^(/video-detail)$ /$key1.html;
}
I want to redirect to a subdomain every call to mydomain.com/blog2, so far I have this:
location ^~ /blog2/$ {
return 301 https://blog.mydomain.com$request_uri$is_args$args;
}
This works only for mydomain.com/blog2/, /blog2 with no trailing slash or /blog2/somethingelse breaks. /blog2/somethingelse/ goes to blog.mydomain.com without the somethingelse part.
How can I achieve:
mydomain.com/blog2 --> blog.mydomain.com
mydomain.com/blog2/ --> blog.mydomain.com
mydomain.com/blog2/something --> blog.mydomain.com/something
mydomain.com/blog2/something/ --> blog.mydomain.com/something
mydomain.com/blog2/something?foo=bar --> blog.mydomain.com/something?foo=bar
If you need to remove the /blog2/ part of the URL, you cannot use $request_uri as it contains the original URI in full. You will need to capture the parts of the URI you need with a regular expression as part of a location or rewrite statement.
Using a regular expression location statement:
location ~ ^/blog2(?:/(.*))?$ {
return 301 https://blog.mydomain.com/$1$is_args$args;
}
Which is probably the same as:
rewrite ^/blog2(?:/(.*))?$ https://blog.mydomain.com/$1 permanent;
See this document for more.