Is it possible to combine these hosts into one?
server {
server_name www.website.com;
rewrite ^ http://website.com$request_uri? permanent;
}
server {
server_name www.website.ru;
rewrite ^ http://website.ru$request_uri? permanent;
}
yes, the following should work:
server {
server_name www.website.com website.com www.website.ru website.ru;
if ( $host ~ "www\.(.*)" ) {
set $hostdomain $1;
rewrite ^ $scheme://$hostdomain$request_uri? permanent;
}
# handling of the non-rewritten non-www requests goes here
}
note: the reason you need to save the domain-minus-www instead of using $1 directly in the rewrite is because the rewrite directive resets the capture variables
Related
I want the Request URL to be converted from
http://host.com/newname/abc?def= to
http://newname/abc?def=
here is the config file
server {
listen 80;
server_name
host.com
location / {
rewrite ^(.+)/$ $1 permanent;
rewrite ^(.+)/index\.html$ $1 permanent;
rewrite ^(.+)\.html$ $1 permanent;
try_files /$host/public/$uri #webserver;
}
}
Adding above line worked form me
location / {
rewrite ^ $scheme://$request_uri? permanent;
}
But it replaces the url in user browser which i donn't want to happen.
Any way to achieve it
You can try this:
location /newname{
proxy_pass http://example.com/newname/abc?def=;
}
or
location /newname{
proxy_pass http://newname/abc?def=;
}
I currently have a domain https://dongyhuynhtantrieu.com and now I want to convert it to https://bacsicare.com, how do I do it? I'm currently using wordpress and nginx. I tried the code but it didn't work.
server {
server_name www.example.com;
rewrite ^ http://example.com$request_uri? permanent;
}
You can use return:
server {
listen 80;
server_name old_domain.com www.old_domain.com;
return 301 http://new_domain.com$request_uri?;
}
Or if inside server block:
if ($request_uri = / ) {
rewrite (.*) http://new_domain.com/ permanent;
}
I am trying to add a simple rule to my NGINX configuration whereby the root url www.example.com is always rewritten with a language suffix www.example.com/en/. I have tried this:
server {
listen 80;
server_name www.example.com;
location / {
rewrite ^$ www.example.com/en/ permanent;
}
...
}
But no luck. Where am I going wrong? Also, is it possible to have a condition where NGINX checks if there is an/en/ suffix and if not, adds one?
EDIT
So I was only one character away from what I wanted initially:
server {
listen 80;
server_name www.example.com;
location / {
# needed the / in between the anchor tags
rewrite ^/$ www.example.com/en/ permanent;
}
...
}
There are two normal ways to deal with redirects:
Using a rewrite rule
server {
...
rewrite ^/$ /en/ permanent;
...
}
Note that rewrite rules do not need to be absolute urls, but if they are to be absolute urls they need to include the protocol rewrite /x https://example.com/y;.
There is no need to put rewrite rules like this in a location block.
Using a location block + return 30x
Using a location block requires using an exact match for the url:
server {
...
location = / {
return 301 /en/;
}
...
}
The use of = means the rule will only match requests for the root of the domain, otherwise the rules of location block precedence mean that the location block would be the default for all requests. Return is used to issue a 301 (permanent) redirect.
In my nginx domain.cong , I wrote the following rewrite rules ...
when a request hits the main domain ( with or without www) it shoudl redirect ot the blog subdomain , but it seems to be wrong ...
server {
....
##### Rewrite rules for domain.tld => www.domain.tld #####
if ($host ~* ^([^.]+\.[^.]+)$) {
set $host_without_www $1;
rewrite ^(.*) $scheme://www.$host_without_www$1 permanent;
}
##### Rewrite rules for www.domain.tld => subdomain.domain.tld #####
if ($host ~* 'www\.[^.]+\.[^.]+$') {
set $host_without_www $1.$2;
rewrite ^(.*) $scheme://subdomain.$host_without_www$1 permanent;
}
...
}
The first rule is correct:
domain.tld => www.domain.tld
but not the second one giving only
www.domain.tld => subdomain.
should be
www.domain.tld => subdomain.domain.tld
Your setup seems a little overcomplicated, and it's not a best practice to match for $host in "if"'s.
If you have only one domain, it's simple:
server {
# ...
server_name domain.tld www.domain.tld;
return 301 $scheme://subdomain.domain.tld$request_uri;
}
server {
server_name subdomain.domain.tld;
# ...
}
If you have many domains, setup is similar, just use regex and capture variables at server_name
I guess I found the solution , modifying the 2nd rewrite rule:
##### Rewrite rules for www.domain.tld => subdomain.domain.tld #####
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://subdomain.$host_without_www$1 permanent;
}
it seems to be running fine until then
I'd like to do an Nginx rewrite where I have two domains: domain.com and domain.net with the following rules:
1) If a user goes to http://www.domain.net/, he will be redirected to http://www.domain.com/
2) If a user goes to http://www.domain.net/anything_else.html the rewrite will not occur.
This is my failed attempt:
server {
listen 80;
server_name www.domain.net domain.net;
location / {
rewrite / http://www.domain.com/ permanent;
}
}
The correct format would be much appreciated!
Unfortunately, #blueberryfields answer didn't quite work for me, had to do it slightly different:
server {
(..)
location / {
rewrite ^(/)$ http://www.domain.com/ permanent;
}
}
Note: Using nginx version 1.1.19
Maybe this works:
server {
listen 80;
server_name www.domain.net domain.net;
location / {
rewrite "^$" http://www.domain.com/ permanent;
}
}