nginx location redirect too many redirects - nginx

I am trying to redirect all URLS from my site /search to /search/grid
using:
location /search {
return 301 /search/grid;
}
this works to redirect /search but then /search/grid gives the HTTP error too many redirects, how can I only redirect if the path is only /search?

Use either and exact match location block, or a rewrite:
location = /search {
return 301 /search/grid;
}
Or:
rewrite ^/search$ /search/grid permanent;
See this and this for more.

Related

Nginx rule to redirect to a blog subdomain just spins

I have tried some Nginx rewrite for my new site
Blog
www.qwerty.com/blog/category/educator-blog/
blog.qwerty.com/educator/
DesiredURL, Actual URL, Nginx_Rule
server {
server_name www.example.com/blog/category/educator-blog/;
return 301 $scheme:// blog.example.com/educator/$request_uri;
}
Also tried this :
location /educator {
rewrite ^/educator(.*)$ http://www.qwerty.com/blog/category/educator-blog/$1 redirect;
rewrite ^/educator-categoryid-(.*)-productid-(.*).htm$ /educator.php?categoryid=$1&productid=$2;
}
I've rebuilt with the applied rules but it seems to just spin, some fixed URL. Does the order of the rules matter?

Nginx location redirect but saving the arguments passed

I am trying to redirect users to a new URL while preserving the URL query parameters. I'm trying this, which doesn't pass the URL params:
location = /api/redirects {
return 301 /api2/redirects;
}
https://example.com/api/redirects?param=1&anotherParam=10
=> https://example.com/api2/redirects
I also tried:
location = /api/redirects {
return 301 /api2/redirects$is_args$args;
}
You can use rewrite explicitly outside of a location block. RegEx parentheses can capture URI values after /api/ as $1. The query string is always passed in a rewrite directive.
# 302 Moved Temporarily
rewrite ^/api/(redirects.*) /api2/$1 redirect;
# 301 Moved Permanently
rewrite ^/api/(redirects.*) /api2/$1 permanent;

Permanent redirection between folders with nginx?

On the nginx configuration file
default
at
/etc/nginx/sites-enabled/
I am trying to redirect requests made to the folder "home" to another folder "jp".
Following the nginx manual,
I tried the script below. Any ideas as to why this would't work? Thanks.
server{
...
server_name _localhost;
location /home/ {
rewrite www.example.io/home/$ www.example.io/home/jp/ permanent;
}
}
You want a permanent redirection from /home/ to /home/jp/.
The first parameter to a rewrite directive is a regular expression which is matched against a normalized URI, in your case /home/.
You can use a rewrite directive, for example:
location /home/ {
rewrite ^/home/$ /home/jp/ permanent;
...
}
Alternatively, you could use an exact match location with a return statement, for example:
location = /home/ {
return 301 /home/jp/;
}

redirect a page to already redirect directory

I have www.example.com and booking.example.com and I want to redirect booking.example.com/partners to example.com/partners.
I'm currently using
location ~ ^/partners/(.*) {
return 301 http://www.example.com/partners/$1;
}
but now I want to redirect an old defunct link to a new one, for example, booking.example.com/partners/doesntexist to www.example.com/partners/doesexist
I tried to do this:
location "^/partners/IDoNotExistAnymore" {
return 301 http://www.example.com/partners/CorrectLink;
}
But it doesn't work, it always redirects to route.
You need to check the syntax of the location directive. See this document for details.
You seem to be using regular expression locations, but prefix locations and exact match locations will be more efficient in this case:
location ^~ /partners {
return 301 http://www.example.com$request_uri;
}
location = /partners/IDoNotExistAnymore {
return 301 http://www.example.com/partners/CorrectLink;
}

Rewrite configuration issue in a Nginx server

I'm configuring an Nginx server with both http and https services. I'm trying to achive the following configuration:
redirect every page to HTTPS, except for the home page
In my "http" server configuration, I have already the second rewrite condition working, but I cannot find the way to write the first.
location = / {
what goes here???
}
location / {
rewrite ^(.*) https://mydomain.com$1 permanent;
}
Ideas?
Zenofo's answer should mostly work (just needs the regex "!~*" instead) but will redirect requests that include the name of the home page along with the others.
Using "$uri" in place of "$request_uri" and spelling out the home page file name in the regex gets around this.
location / {
if ($uri !~* ^/index.html)
{
# Redirect non home page requests
rewrite ^ https://$host$request_uri? permanent;
}
# Process homepage requests
....
}
If running php where everything goes through index.php (front end controller) then you can use
location / {
if ($uri !~* ^/index.php$)
{
# Redirect non home page requests
rewrite ^ https://$host$request_uri? permanent;
}
# Process homepage requests
....
}
using $request_uri,like this: (I haven't tested)
location / {
if ($request_uri != ^/$)
{
rewrite ^(.*) https://mydomain.com$1 permanent;
}
}

Resources