Need to insert /#/ into a url - nginx

I am trying to create a try_files or rewrite rule. Only for a particular url with a query
http://example.com/survey?id=5 becomes http://example.com/survey/#/?id=5

This will do it:
location =/server {
rewrite ^ $scheme://example.com/server/# permanent;
}
Using $scheme preserves the scheme of the request (http or https).
Using location =/server matches requests to /server and you need the = to match only exact matches otherwise you'll keep getting redirected.

Related

Remove part url use nginx [duplicate]

I have a web server which accepts https://www.example.com/API/ooo/xxx, https://www.example.com/API/aaa/bbb, and https://www.example.com/API/xxx/yyy ... etc.
Now we want to redirect the above quests to https://www.example.com/ooo/xxx, https://www.example.com/aaa/bbb, and https://www.example.com/xxx/yyy ... etc.
I've tried using rewrite key word in nginx:
location /API/ {
rewrite ^/API(.*)$ https://$host$1 redirect;
}
This works for GET requests. But it turns POST requests to GET requests. This is something I don't want.
How can I preserve the http method while redirecting /API/* to /*?
This post says that I can use 307 redirect. But rewrite doesn't seem to support 307 redirect. And I can't figure out how to use $1 regular expression property in return.
Use a return statement. You can use a regular expression location block to capture the part of the URI to return.
For example:
location ~ ^/API(/.*)$ {
return 307 $1$is_args$args;
}
Note that with the regular expression location directive, its order within the configuration is significant, so you may need to move it. See this document for details.

nginx rewrite preserving CGI query params (with a hash anchor)

For my www.example.com nginx config, I have these rewrite rules:
rewrite ^/foo$ https://one.example.com/page#one permanent;
rewrite ^/foo(\?.*)$ https://two.example.com/page$1#two permanent;
rewrite ^/bar$ https://three.example.com/page#one permanent;
rewrite ^/bar\?(.*)$ https://four.example.com/page?$1#two permanent;
A request for http://www.example.com/foo correctly redirects to https://one.example.com/page#one.
A request for http://www.example.com/bar correctly redirects to https://three.example.com/page#one.
A request for http://www.example.com/foo?extra=yes incorrectly redirects to https://one.example.com/page#one?extra=yes (I expect it to go to https://two.example.com/page?extra=yes#two).
A request for http://www.example.com/bar?extra=yes incorrectly redirects to https://three.example.com/page#one?extra=yes (I expect it to go to https://four.example.com/page?extra=yes#two).
How can I redirect to a page copying CGI parameters and linking to a particular anchor in the destination page?
It appears that the rewrite directive does not handle the # fragment correctly when assembling the query string into the replacement string.
You can prevent rewrite from appending the query string by adding a trailing ? to the replacement string. So, you can construct the correct result using the built-in variables $is_args and $args.
For example:
rewrite ^/foo$ https://one.example.com/page$is_args$args#one? permanent;
See this document for details.
Note that the query string is not part of the normalised URI used to match rewrite and location statements, so your ^/foo(\?.*)$ regular expression will not work.

Nginx remove URL path and place it as a query parameter

I have a URL like so: https://example.org/v2?product=lifesum and I need to rewrite it to be: https://example.org?version=v2&product=lifesum. The URL may have more or less query params, so I need to keep all of those. Also, the /v2 may actually not be present, so I need to handle those cases. Here are some examples of how this should be rewritten:
https://example.org/v2?product=lifesum ->
https://example.org?version=v2&product=lifesum
https://example.org?product=lifesum ->
https://example.org?product=lifesum
https://example.org/v13/foo/bar?product=lifesum -> https://example.org/foo/bar?version=v13&product=lifesum
https://example.org/v1113 -> https://example.org?version=v1113
https://example.org -> https://example.org
Here is what I have tried so far, but it is not working:
# HTTP Server
server {
# port to listen on. Can also be set to an IP:PORT
listen 8080;
# This is my attempt to match and rewrite
location ~* (\/v\d+) {
rewrite (\/v\d+) /?api_version=$1 break;
}
location = / {
# I have also tried this rewrite but iit is not working either
rewrite (\/v\d+) /?api_version=$1 break;
try_files $uri $uri/ /index.html;
}
}
NOTE: This is a Single Page Application, if that helps.
To meet all of your requirements, you will need to capture that part of the URI which follows the version string.
For example:
rewrite ^/(v\d+)(?:/(.*))?$ /$2?version=$1 redirect;
The redirect flag causes Nginx to use an external redirect with a 302 status (see this document for details). An external redirect is necessary for the SPA to see the new URI.
The rewrite statement can be placed in the outer server block or within a location block that matches the original URI (for example: location ~* ^/v\d).
To avoid Nginx adding a port number to the redirected URI, use:
port_in_redirect off;
See this document for details.

Nginx redirect to different location with part of the request header

I want to redirect certain requests to different location, but with part of the request header, example:
https://example.com/something/value ---> https://example.com/something/index.php?var=value
pseudo-code:
location ^~ /something/$value {
return 301 https://$host/something/index.php?var=$value;
}
The location only needs to match that part of the URI which is constant. If /something/index.php and /something/value is the same prefix, then you do not want to use the ^~ modifier, otherwise the PHP file will not be found. See this document for details.
Use rewrite to capture the "value" part of the URI and append it as an argument.
For example:
location /something/ {
rewrite ^/something/(.*)$ /something/index.php?var=$1 last;
}
If you want an external redirection use rewrite...permanent instead of rewrite...last. See this document for details.

Chage the part of the URL using nginx

I m using nginx webserver.
I want to change the url before it hits the server from
https://www.example.com/abc/contact-us
to
https://www.example.com/#/contact-us
Thanks in advance.
For a single URI redirection, an exact match location and return statement may be most efficient:
location = /abc/contact-us {
return 301 /#/contact-us;
}
To redirect all URIs beginning with /abc use a rewrite directive:
location ^~ /abc/ {
rewrite ^/abc(.*)$ /#$1 permanent;
}
The location block is largely redundant, but means nginx only looks at the regular expression when it needs to. See this document for more.

Resources