Nginx rewrite only when root domain - nginx

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;
}
}

Related

How to edit url without updating in browser - nginx config

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=;
}

How to redirect from old domain to new domain on nginx

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;
}

nginx how do I redirect a url to a specific page

I have looked at this question which describes how to redirect a url in nginx, as shown below
# main server block for www.test.com
server {
listen 80;
server_name www.test.com;
...
}
# redirect test.com to www.test.com
server {
server_name test.com;
return 301 $scheme://www.test.com$request_uri;
}
What I need to do is redirect a set of individual pages, so was wondering how to do this
e.g. test.com\index , test.com\home , test.com\main to test.com\index.php
Then I have some other pages to simply redirect simply to the .php extension
e.g. test.com\about to \test.com\about.php
e.g. test.com\contact to \test.com\contact.php
What is the best way to do this?
Found the answer... assuming the following server block for test.com
server {
listen 80;
server_name www.test.com;
...
}
Add the appropriate regex location path, and rewrite or return to the redirect url.
for test.com\index , test.com\home , test.com\main to test.com\index.php
location ~ ^/(index|home|main) {
rewrite ^/.* http://$server_name/index.php permanent;
}
for test.com\about to \test.com\about.php
location /about {
rewrite ^/.* http://$server_name/about.php permanent;
}

Pass multiple URL parameters in Nginx rewrite

I'm trying to redirect www.example.com/page to www.example.com/something/some-stuff?foo=bar&foo2=bar2 in Nginx but can't seem to do it properly.
This is what I have so far:
server {
listen 80;
server_name example.com
www.example.com;
return ^/page /somepage/some-stuff?foo=bar&foo2=bar2 permanent;
}
What's wrong with the above server block?
This worked!
location /page {
rewrite ^/page http://$http_host/somepage/some-stuff?foo=bar&foo2=bar2 redirect;
}

Combining two nginx hosts into one

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

Resources