Redirect in Nginx variable subdomain - nginx

I have a bunch of subdomain http://product.domain.com that I would like to redirect to http://www.domain.com/product.
Of course, the product name can be different, and the redirect has to be done accordingly.
Any pointers?
Thanks

try something that could include several subdomains, like this
server {
server_name ~^(sub1|sub2|sub3|sub4).example.com;
return 301 $scheme://example.com/$1;
}

Try this:
server {
listen 80;
server_name product.domain.com;
return 301 http://www.domain.com/product$request_uri;
}

Related

Capture variable nginx if statement

I am working on a site i have since long time and i would kike to redirect old useless pages to a category page listing relevant articles.
I have seen n my google search console the following url :
https://www.example.com/Greece/sport%20?page=55
I would like to get :
https://www.example.com/greece.html?page=55
I am using nginx with a custom script that imposes in the server setting to indicate for which domain the redirection has to be done.
I am trying to figure out the way to do it and the following code is not giving me the right redirection.
I would appreciate if anybody could give me a hint.
location ^~ /(.*)/ {
if ($host ~* ^(www\.)?(mysite\.com)$) {
return 301 $scheme://$host/$1.html;
}
return 404;
}
Thanks
Try this config:
server {
...
server_name mysite.com www.mysite.com;
location ~* ^(/[^/]+)/ {
return 301 $scheme://$host$1.html$is_args$args;
}
....
}
The regular expression in location block you can modify as you need.

How do I redirect certain paths with wildcards with Nginx?

For example I need to redirect urls that match this pattern and keep the rest.
aaa.mydomain.com/path/1/listing
to
bbb.somedomain.com/path/1/listing
The "1" in that path can be any integer and needs to go to the correct redirect. Can this be done with a location block or do i need rewrite?
You can do it like this
server {
server_name aaa.mydomain.com;
return 301 //bbb.somedomain.com$request_uri;
}
UPDATE
Well, then use something like this
location ~ /path/[^/]+/listing$ {
return 301 https://bbb.somedomain.com$request_uri;
}

Multiple domains - subdomains routed to main domain

I have multiple domains connected to the same DO droplet, with nginx
Let's assume:
firstdomain.com
seconddomain.com
I would like to set my nginx in a way that, every subdomain will be directed to it's main domain, and it will be reflected in address bar too:
subdomain.firstdomain.com ----> firstdomain.com
asldk.firstdomain.com --------> firstdomain.com
test.seconddomain.com --------> seconddomain.com
and so on.
What is the simplest way to achieve this?
server {
listen 80;
server_name ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
if ($subdomains != "") {
rewrite ^/(.*)$ http://$domain/$1;
}
}
I want to post this answer, for future visitors and for myself (I'm sure I will forget)
server {
listen 80 default_server;
server_name ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
if ($subdomains != "") {
rewrite ^/(.*)$ http://$domain/$1;
}
index index.html;
root /etc/nginx/conf.d/404;
}
It is almost identical with the solution I accepted, and it has a defined 404 page in conf.d to modify default nginx page.

Nginx redirect from one domain to another?

I'm on the process of migrating the same app but to a different domain.
For the old domain, I've the routes as:
http://app.example.com/app/users/sign_in?query=hello
I want it to be redirected to another domain omitting the app part as:
http://app.newexample.com/users/sign_in?query=hello
I tried with:
server {
...
location /app {
rewrite ^$scheme://app.sparkon.com/app(/.*)$ $1 last;
}
...
}
I doesn't work. How to achieve this?
I had this issue about a year ago and spent a long time looking for solutions. I found and use this:
server {
listen 80;
server_name example.com app.example.com;
rewrite ^/app(.*)$ http://app.newexample.com$1 permanent;
}
server {
listen 80;
server_name app.newexample.com;
# config for primary domain here
}
From this link. Credit to them.
Don't put scheme in the rewrite pattern:
server {
server_name app.example.com;
location /app/ {
rewrite ^/app/(.*)$ http://app.newexample.com/$1;
}
}
Brg.
I prefer this method as it doesn't need to use rewrite, one of the things that i read are good to avoid too much, cause it needs more processing by the nginx engine.
location ~ /app/(.*) {
return 301 $scheme://app.sparkon.com/$1;
}

Nginx Rewrite Rule

I need to write a generic nginx rule such that
subdomainmysite.in is redirected to www.subdomainmysite.in
randommysite.in is redirected to www.randommysite.in]
xyzmysite.in is redirected to www.xyzmysite.in
and so on
please help
server {
server_name ~^(?!www\.);
return 301 http://www.$host/;
}
http://nginx.org/en/docs/http/server_names.html
http://nginx.org/r/server_name
http://nginx.org/r/return
man pcrepattern
man pcresyntax

Resources