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.
Related
Is it possible to make this kind of url rewrite?
Request url:
https://image.domain.com/listing/1_10_iP2LROSEafC01584630756.jpg
Rewrite to:
https://image.domain.com/images/i/P/2/L/iP2LROSEafC01584630756.jpg
If /images/i/P/2/L/iP2LROSEafC01584630756.jpg exists in server, it's served, if not then query is redirected to php file /make_image.php
Thank you!
Request url: https://image.domain.com/listing/1_10_iP2LROSEafC01584630756.jpg Rewrite to: https://image.domain.com/images/i/P/2/L/iP2LROSEafC01584630756.jpg
If you mean an external redirect, then this is achieved using the rewrite directive. Place the statement in the server block, or within a location block that handles these kinds of request (i.e. URIs beginning with /listing/ and/or ending with .jpg).
Your question does not state what 1_10_ means, but let's assume that you want the two arbitrary numbers ignored.
For example:
rewrite ^/listing/\d+_\d+_(.)(.)(.)(.)(.*)$ /images/$1/$2/$3/$4/$5 permanent;
See this document for details.
If /images/i/P/2/L/iP2LROSEafC01584630756.jpg exists in server, it's served, if not then query is redirected to php file /make_image.php
This is achieved using a try_files statement within a location block that processes requests for URIs beginning with /images/.
For example:
location /images/ {
try_files $uri /make_image.php;
}
See this document for details.
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.
I have URLS in this format:
/wiki/index.php?title=Widget
/wiki/index.php?title=Blue
/wiki/index.php?title=Red
/wiki/index.php?title=Etc
I want to be able to match any URL that has the pattern "/wiki/index.php?title=" or even just "/wiki/index.php" (but so it will pick up the above URLs with the "?") and then redirect them to simply /wiki/ (all pages that match the above pattern go to the single url /wiki/)
I used to have a Mediawiki install on the /wiki/ directory with a lot of pages in the format above. However now I am running a Wordpress install and it is using the /wiki/ directory. I don't need each rewritten URL to go to a different URL (I know that is difficult as my source URLs have parameters) but right now they all 404 and so I just want to direct to them /wiki/ at least.
Simple URL rewriting
The easiest case (rewrite all /wiki/index.php requests with any arguments) can be done by this config (inside your server block):
location = /wiki/index.php {
rewrite .* /wiki/?;
}
The '?' sign at the end of second rewrite parameter is a trick to completely remove any request arguments. Without it, request /wiki/index.php?title=Widget will be rewrited to /wiki/?title=Widget.
Rewriting only requests matching /wiki/index.php?title=... is a more complex, I don't know how to do it without if construction:
location = /wiki/index.php {
if ($request_uri ~ ^/wiki/index\.php\?title=) {
rewrite .* /wiki/?;
}
}
If you want to generate HTTP 301 or 302 redirect instead of simple URL rewriting, you can use redirect (for 301 temporary redirect) or permanent (for 302 permanent redirect) flag at the end of rewrite directive parameters (see documentation).
Rewriting URLs to individual pages
This task is not as difficult as it seems. For redirecting /wiki/index.php?title=Widget to /wiki/Widget, /wiki/index.php?title=Blue to /wiki/Blue etc. we can make use of map directive:
map $request_uri $uri_suffix {
~^/wiki/index\.php\?title=([^&]*) $1;
}
server {
...
location = /wiki/index.php {
rewrite .* /wiki/$uri_suffix?;
}
...
}
Please note that map directive must be declared outside your server configuration block!
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.
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.