Nginx URL rewrite for api/rest to rest/v1 - nginx

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;
}

Related

Nginx Configuration for Redirecting URL

I want to redirect something like this /atpf/xyz -> /atp/xyz, I have a configuration like this, but doesn't seem to work
location /atpfe {
rewrite ^/atp/(.*)$ /$1 break;
proxy_pass http://backend-atp;
}
Can someone suggest?
I changed it to this and worked:
location /atpfe {
rewrite ^/atpfe/(.*)$ /atp/$1 break;
proxy_pass http://backend-atppub;
}
Wasn't fully aware of the rewrite functionality

NGINX location rewrite for WP permalink

So, I have WP with almost classic permalink structure, like: /%category%/%year%/%month%/%day%/%postname%/
I would like to redirect with 301 error code to new style, like /%year%/%month%/%day%/%postname%/ or simple /%postname%/.
I've made this rule, but they return same URL.
location ~ "^\/([a-zA-Z0-9_.-]+)\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/([a-zA-Z0-9_.-]+)\/$" {
rewrite ^(.*) $1 permanent;
}
Or, for /%postname%/ I've tried code like this:
location ~ "^\/([a-zA-Z0-9_.-]+)\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/([a-zA-Z0-9_.-]+)\/$" {
rewrite ^(.*) $5 permanent;
}
But this code return empty response.
Please, help me solve the issue. I can't catch were I'm wrong. Thanks!
So, it was very simple:
location ~ "^/([a-zA-Z0-9_.-]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/([a-zA-Z0-9_.-]+)/$" {
rewrite ^(.*)/(.*)/(.*)/(.*)/(.*)/ /$5/ permanent;
}
This rule rewrite /category/year/month/day/postname/ to /postname/ URL in WP with 301 error code for search engines.

Nginx rewrite not working as per docs

I am trying to redirect http://example.com/test/index.php?/Server/Search/IP_ADDRESS to http://example.com/?ip=IP_ADDRESS
Tried following rewrite rule, but, it is not working.
rewrite ^/test/index.php?/Server/Search/(.*)$ http://example.com/?ip=$1 redirect;
This is needed for internal purpose as another api will check the IP address at http://example.com/?ip=IP_ADDRESS
location = /test/index.php {
if ($args ~ "^/Server/Search/(.+)") {
set $sip $1;
rewrite ^(.*)$ /?ip=$sip? redirect;
}
}

how do I rewrite a URL in nginx to an updated location?

I have the following URLs:
1) http://example.com/downloads/
2) http://example.com/downloads/widgets
3) http://example.com/downloads/gadgets
These URLs need to be redirected rewritten to the following:
1) http://example.com/products/
2 & 3 & etc) http://example.com/products/thingies
I'm currently trying the following nginx code:
location ~* ^/downloads/ {
rewrite ^/downloads/$ /products break;
rewrite ^/downloads/(.*)$ /products/thingies break;
}
It's almost working, however my site's document root is /var/www/example.com/public. So after processing the rewrite rules, nginx tries to literally serve /var/www/example.com/public/products/, whereas I want it to just rewrite to http://example.com/products/ (which then proxies to PHP and so on).
Where am I failing? Is there a different, better way to accomplish this?
Thank you for any help.
-- UPDATE --
I got it to work by using the following rules:
rewrite ^/downloads/?$ $scheme://$host/tools last;
location ~* ^/downloads/ {
rewrite ^/downloads/?$ $scheme://$host/products last;
rewrite ^/downloads/(.*)$ $scheme://$host/products/thingies last;
}
IS this the proper way of doing it in nginx? I haven't seen this rewrite rule format anywhere while researching this. It somehow seems odd.
Your update redirects, not rewrites.
Here is how I would do:
location /downloads/ {
rewrite ^ /products/thingies;
}
location = /downloads/ {
rewrite ^ /products/;
}
# uncomment if you need '/downloads' (without trailing slash) act as '/downloads/'
#location = /downloads {
# rewrite ^ /products/;
#}
The correct syntax appears to be:
location ~* ^/downloads/ {
rewrite ^/downloads/?$ /products permanent;
rewrite ^/downloads/(.*)$ /products/thingies permanent;
}

Nginx Rewrite: change only index location?

How can I send a user to a new location, only if the user have no URI? I am trying the follow, but it does not works... it always send me to /newlocation
rewrite ^/$ http://www.domain.com/newlocation permanent;
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
So basically, what I need is:
If the user writes on browser www.domain.org it sends to www.domain.com/newlocation
If the user writes on browser www.domain.org/something it sends to www.domain.com/something
Thanks!
I'm not sure why your current approach isn't working. ^/$ should only match /. Maybe it's something else it the current config. Here's a server that should do what you want.
server {
server_name www.domain.org;
# Only match requests for /
location = / {
rewrite ^ http://www.domain.com/newlocation permanent;
}
# Match everything else
location / {
rewrite ^ http://www.domain.com$request_uri? permanent;
}
}

Resources