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"
Related
Nginx + apache is running. One subdomain was liquidated and its articles moved to the same addresses, but a new subdomain.
In the .conf of the old subdomain, I have prescribed the following settings, they work fine.
server
{
listen 80;
root /var/www/old.site.ru;
server_name old.site.ru www.old.site.ru;
return 301 https://new.site.ru$request_uri;
}
server
{
listen 443;
ssl on;
ssl_certificate /etc/ssl/wildcard.crt;
ssl_certificate_key /etc/ssl/wildcard.key;
server_name old.site.ru www.old.site.ru;
return 301 https://new.site.ru$request_uri;
}
Now it is planned to delete several more subdomains with the transfer of their articles to new ones, and I decided to delete unnecessary old subdomains. I want to redirect from specific (but already non-existent) subdomains to specific new ones in the general config. How can I do that?
I got into nginx.conf - something doesn't start in any way.
What do I need to do? Thanks!
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
Could be the stupid one and could be most duplicate question ever asked.
But yes, I can't get this done.
I've one domain which is : example.co.in
I want to redirect to www.example.co.in
Server is : NGINX
My vHost config is:
server {
listen 80;
server_name example.co.in www.example.co.in;
return 301 http://www.example.co.in$request_uri;
}
server {
listen 80;
server_name example.co.in www.example.co.in;
access_log /var/www/example.co.in/public_html/logs/access.log;
error_log /var/www/example.co.in/public_html/logs/error.log;
....
}
I've a A record entry for www in my dns server.
Edit:
I'm getting ERR_TOO_MANY_REDIRECTS error
Any help would be help full.
You have to server blocks with the same port and server_names. It hits the first one and tells the requester to redirect to www which the requester makes another request to the same server block which starts the process over again.
If you want it to redirect to www then take www out of the first server block then reload or restart nginx.
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
}
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;