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
Related
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/;
}
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.
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.
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;
}
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;
}