Permanent redirect from example.com/ to https://www.example.com/dir/ - redirect

I have some software build with microservice philosophy. On of them - which was hosted on example.com/ became useless so I want the user to be redirected to example.com/dir/ where another service is hosted. The most popular solution on StackOverflow is to use the following code in the configuration file:
location = / {
return 301 $scheme://$http_host/dir/;
}
or
location = / {
return 301 https://$http_host/dir/;
}
They both fail on my server - the server returns 503 - to many computations. The stacktrace shows, that there is no rule-infinite-rule so this code might be returned by load-balancer.
Is there any other well working solution for this issue? StackOverflow and ServerFault have just been carefully searched for the last 3 days by me and any solution worked.

After researching together with my teammates it has occured, that not only return 301 url; succesfuly redirects user. Although the documentation says avoid using rewrite what has worked is:
server {
...
rewrite ^/$ https://$http_host/dir/ permantent;
...
}
The permantent statement is responsible for acting as a regular redirection.
Important:
In nginx ^ is the begining of a regular expression and of the incomming string and $ sign is a normal regex sign which stands for theEndOfString. So regex ^/$ means match iff url equals /.
Credits:
Hopefully this will save one's weekend-time and money.

The = sign is invalid.
location / {
return 301 https://$http_host/dir/;
}
But that will end in a redirect loop, so be more specific:
location ~ ^[/]?$ {
return 301 https://$http_host/dir/;
}

Related

Nginx: one directory should be redirected below itself

I have been struggling with a simple problem with nginx redirecting, but to avail so far.
I have two (language based) domains:
server {
server_name a.b.com;
.
.
.
}
server {
server_name d.b.com;
.
.
.
}
Now on d.b.com/example, should redirect to d.b.com/example/right. I have tried to build a location like below (plus a lot of variations of it including full urls).
location ^/example {
redirect /example /example/right permanent;
}
Nginx allows the syntax, but when I try to open the page, I always get the 404.
I think this should be straight-forward but my brain just can't get it right, nor have I found the solution in the web.
The biggest problem is that the end users already sent the mass mailing to our customers with the invalid link (without even checking it!), so I am a bit hard pressed with timetable...
wbr
hank
If you are trying to match a single URI, use an exact match location:
location = /example {
return 301 /example/right;
}
See this document for details.

Nginx redirect some sub folders

Can anyone point me in the right direction. I would like to redirect some links but not the root directory. For example:
example.com/post-on-something
example.com/post-on-something-else
redirects to the new domain:
newdomain.com/post-on-something
newdomain.com/post-on-something-else
however I would like
example.com to show the new website (which is a single page)
Because I have a large number of posts I don't know what each page will be and I would like to avoid a rewrite rule for every old post as this would hit well over 100.
So far I have:
location / {
return 302 https://www.newdomain.com/$request_uri;
}
But this does everything.
Thanks for your help,
Tom.
You're nearly there... just need to use the syntax for an exact match
location = / {
root /data/www;
}
location / {
return 302 https://www.newdomain.com/$request_uri;
}

how to make this nginx location redirect not case sensitive

my situation:
domain.com is a WordPress site (and WP works fine). its installed at the root.
I also created/add a sub folder: domain.com/subfolder
that has an index.php page in it, like so:
domain.com/subfolder/index.php
I need any URL entered into browser to redirect to subfolder index.php page.
I have this nginx location, but it ONLY works if you type in case sensitive subfolder or subfolder/, I need non-sensitive:
location /subfolder {
return 301 http://domain.com/subfolder/index.php;
}
location /subfolder/ {
return 301 http://domain.com/subfolder/index.php;
}
SO basically, if user types anything except subfolder or subfolder/, let WP take care of the url. otherwise, redirect it.
I've tried ~ ^ *, but can't get it to work right.
what am i doing wrong? :)
thanks!
PS. if you see something else that I should be doing, please let me know -- i'm new to nginx
Try this:
location ~* /subfolder/ {
return 301 http://domain.com/subfolder/index.php;
}
The tilde and asterisks ensure that this location will be matched case insensitive.

how to do nginx rewrites from a url with get parameters

This is my first ever StackOverflow question so please bear with me.
Nginx serves many different sites for us and we have a lot of redirects from migrating clients from different vendors and such. We have set up an /includes directory that houses redirect files for each domain that we migrate over. Occasionally, we will need to write redirects from a url that contains get parameters:
http://example.com/content/default.aspx?NewsId=28
To do this, we have been doing this in an nginx /includes file called example.com-redirects
location ^~ /content/default.aspx {
if ($args ~ "NewsId=28") { rewrite ^ http://example.com/news? permanent; break; }
# add more statements like the one above
}
That has worked just fine for us thus far. Unfortunately, we need to do the same thing but for a different domain that could have the same get parameters. And of course nginx doesn't allow for duplicate locations.
location ^~ /content/default.aspx {
if ($args ~ "NewsId=28") { rewrite ^ http://differentexample.org/news? permanent; break; }
}
I've tried a couple of different solutions all giving me syntax errors. No one at my company is an nginx expert anymore so I could really use some help solving this. I have added an if ($host ~ "example.com") within the location block and that gave me an error. And I've tried adding the location block within the if ($host ~ "example.com") block. Both times nginx told me that I can't put that there.
I usually find my answer in the vast knowledge base that is the internet but seem to be striking out on a solution for this and we're running out of time before we launch this client.
Instead of using if, use a variable. In the virtual host config you set this variable. In the include with the location you use it:
server {
...
set $redirect_host "example.org";
include /includes/news28.conf;
...
}
# include part
location /foo {
if ($args ~ "NewsId=28") { rewrite $scheme://$redirect_host/news? permanent; }
}
Of course you need to set that variable also in the server config of the host already using this include. Hope this helps.

How to set up nested subfolder to subdomain Nginx 301 redirects

I'm trying to set up Nginx redirects that will rewrite any URL from several older forum setups to a new forum. The older forums ran from subfolders, while the current forum is running from a subdomain of the same site.
So, for example, I want ANY request to site.com/ask to be redirected to the front page of forum.site.com. Since I'm dealing with 3 old forums, I tried to set up a nested redirect like this:
location ~ ^/\~([^/]+)/(.*)$ {
location ~ ^/\~ask/(.*)$ {
rewrite ^(.*)$ http://forum.site.com$1 permanent;
}
location ~ ^/\~forum/(.*)$ {
rewrite ^(.*)$ http://forum.site.com$1 permanent;
}
location ~ ^/\~qa/(.*)$ {
rewrite ^(.*)$ http://forum.site.com$1 permanent;
}
}
With the above rules, only the first one works and partially. For example, a request to site.com/ask gets redirected to forum.site.com, which is fine, but any request to, say, site.com/ask/what-is-this goes to forum.site.com/404.
Request to site.com/forum and site.com/qa do not work at all.
I'm sure there's a simpler way of doing this, but I don't want to spend several days trying to figure it out.
Your input is welcome and appreciated.
Edit:
Not getting anywhere with the above code, I reduced it to this:
location ~ ^/\~([^/]+)/(.*)$ {
location ~ ^/\~(qa|forum|ask)/(.*)$ {
rewrite ^/~(qa|forum|ask)/(.*)$ http://forum.site.com$1 permanent;
}
}
But the result is still the same. Any ideas?
Actually you want to redirect user to the main page of your forum regardless of what page he came to. Try this.
location /qa/ {
return 301 http://forum.site.com;
}
location /ask/ {
return 301 http://forum.site.com;
}
location /forum/ {
return 301 http://forum.site.com;
}