Nginx URL forwarder to another domain - nginx

How can I redirect someone from one domain to another whilst keeping the current URL path?
For example:
example.com/ABC -> example2.com/file.html?x=ABC

You can create a rewrite rule to forward from the old domain to the new one.
server {
listen 80;
server_name example.com;
return 301 http://new.example.com$request_uri;
}
Documentation: http://nginx.org/en/docs/http/converting_rewrite_rules.html

Related

Nginx Mess (URL Redirects)

I'm looking to redirect old urls to the new urls / new links. It's proving to be a mess to do just that. I'm using nginx 1.19.6 and ubuntu 18.04
I want to setup my redirects so https://ikhnetworks.com/feeds/Live/* would redirect to https://ikhnetworks.com/feeds/HD1/
https://ikhnetworks.com/Stations/* -> redirect to the respective call letters - for example
https://ikhnetworks.com/Stations/Radio/WDJO/ > https://ikhnetworks.com/WDJO/
For the first part, I can provide you the solution as:
server{
listen 443;
server_name ikhnetworks.com;
...
...
root /var/www/html;
location /feeds/Live {
return 301 https://ikhnetworks.com/feeds/HD1/;
}
}

Redirect root to another folder in nginx server

I am very new to nginx. I was using Apache previously and was using htaccess to redirect root to another folder. Now migrated to nginx. Here is four things I want to achieve
Redirect http to https e.g. http://www.example.com -> https://www.example.com
Then redirect root to another folder but URL must be rewritten as example.com not example.com/blog
All files in php should show as html in url e.g. example.com/contact.php -> example.com/contact.html
example.com/page.php?content=file -> example.com/file
I found this code to redirect but don't know where to insert this code nginx.conf or any other file?
server{
location = / {
return 301 https://www.example.com/blog;
}
}
Also please suggest me if these changes are made in nginx.conf file or /etc/nginx/sites-available/example.com file.
To redirect HTTP to HTTPS traffic you can create another server block to match the incoming HTTP and domain then rewrite to your HTTPS server block.
Which file do you put this in? Both /etc/nginx/nginx.conf and /etc/nginx/sites-available/example.com should get read (unless you changed config) so it shouldn't matter, but I personally put these configs in /etc/nginx/sites-available/example.com because I consider it part of the same domain.
file: /etc/nginx/sites-available/example.com
server {
listen 80;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
...
# your location blocks and redirects here
}

nginx - Wildcard domain force www

I know I can redirect a non-www from a specific domain to a www version by doing:
server {
listen 80;
server_name domain.com;
return 301 $scheme://www.domain.com$request_uri;
}
But what if I want to catch any domain that does not have a subdomain already set (like help.domain.com) to redirect to www, like newdomain.com redirecting to www.newdomain.com. The trick here is I want to catch any domain pointed towards the server, not ones that I "know of"

nginx rewrite for subsubdomains to subdomains

We have an application where we use subdomains for each of our customer's installations. so we have customer1.ourapp.com, customer2.ourapp.com, customer3.ourapp.com and so on.
Because of security we want to redirect all http to https, since we have a wildcard SSL certificate.
Also, some customers are not that tech savvy and add www to their domain name, so then you get things like: http://www.customer1.ourapp.com or https://www.customer1.ourapp.com. In those cases the SSL certificate isn't valid because of the subsubdomain.
I'm trying to write the vhost config for nginx to make the correct redirect in these two cases. I got the http to https redirect to work with:
server {
listen 80;
server_name *.ourapp.com;
#Rewrite all nonssl requests to ssl.
return 301 https://$host$request_uri$is_args$args;
}
correct url's use:
server {
listen 443;
server_name *.ourapp.com;
#Rest of config
}
Made an attempt for the subsub domains, but it's not matching:
server {
server_name "~^(.*)\.(.*)\.ourapp\.com$";
return 301 https://$2.ourapp.com$request_uri;
}
Any idea how to get this working?
Wildcarded server takes precedence over regexp'ed one and matches 'www...' too.
You can use one definition for both cases:
server_name ~ ^(?:.*\.)?(.*)\.ourapp\.com$;
return 301 https://$1.ourapp.com$request_uri;

Nginx subdomains with www

I have Nginx configured in a Amazon EC2 server.
Right now both www.myserver.com, .myserver.com works perfect.
What I need, is configure www..myserver.com. I need to redirect the user to .myserver.com. I mean, I need to rewrite the url or something.
How can I do that?
server {
listen 80;
server_name www.myserver.com;
return 301 http://myserver.com$request_uri;
}
You don't need "rewrites" in nginx. Leave them to apache, and read the docs:
http://nginx.org/en/docs/http/server_names.html
http://nginx.org/en/docs/http/request_processing.html
http://nginx.org/en/docs/http/converting_rewrite_rules.html
Upd:
That is not clear from your question (who knows, what do you mean by double dots). But if you have ever tried to read the docs, you would easily modify the example for your needs:
server {
listen 80;
server_name ~^www\.([^.]+\.myserver\.com)$;
return 301 http://$1$request_uri;
}

Resources