Nginx & url rewriting - nginx

I have a redirection:
server {
server_name mydomain.com;
rewrite ^(.*) http://myanotherdomain.net/image$1 permanent;
}
Everything is ok when I go in mydomain.com/exemple, but how to point mydomain.com where I want ?
I want:
mydomain.com/test -> myanotherdomain.net/image/test (or other than "test") and
mydomain.com -> myanotherdomain.net
Is it possible ?

server {
server_name mydomain.com;
location = / {
return 301 http://myanotherdomain.net;
}
location / {
return 301 http://myanotherdomain.net/image$request_uri;
}
}

Related

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 Reverse Proxy Multiple Applications on One Domain

like these:
test1.sec.com 192.168.1.8:8001<br>
test2.sec.com 192.168.1.8:8002<br>
test3.sec.com 192.168.1.8:8003<br>
http://www.sec.com/test1/ 192.168.1.8:8001<br>
http://www.sec.com/test2/ 192.168.1.8:8002<br>
http://www.sec.com/test3/ 192.168.1.8:8003<br>
how to config the nginx.conf ?
You haven't provided much information, but based on what you gave, this should work:
server {
listen 80;
server_name test1.sec.com;
location / {
proxy_pass http://192.168.1.8:8001;
}
}
server {
listen 80;
server_name test2.sec.com;
location / {
proxy_pass http://192.168.1.8:8002;
}
}
server {
listen 80;
server_name test3.sec.com;
location / {
proxy_pass http://192.168.1.8:8003;
}
}
Then, for your www.sec.com, you'll need to add separate location blocks to catch the /test/ URIs.
server {
listen 80;
server_name www.sec.com;
location /test1/ {
redirect 301 $scheme://test1.sec.com;
}
location /test2/ {
redirect 301 $scheme://test2.sec.com;
}
location /test3/ {
redirect 301 $scheme://test3.sec.com;
}
#Rest of your config here...
}
Note: You have to specify your test location blocks before your root (/) unless you use a modifier to give them precedence.

Simple url rewriting

I have a simple index.html page with some javascript.
On my nginx config, I would like to apply this rule :
Rewrite http://www.mywebsite.com/?l=A1B2C3 to http://www.mywebsite.com/A1B2C3
Here is my file (I got a 500 error) :
server {
listen 80;
root ...;
index index.html;
server_name www.mywebsite.com;
location / {
rewrite ^(.*)$ /?l=$1 last;
break;
}
}
Try this:
location / {
rewrite ^/([A-Za-z0-9_]+)$ /?l=$1 last;
}

How to set up Nginx config to rewrite my subdomain

I'm try to set up the subdomains with Nginx, but I get some error. The following is my config:
server {
listen 80;
server_name dimain.com *.domain.com;
root /path/to/fuelphp_project/public;
location / {
index index.php index.html index.htm;
if ($host ~ (.*)\.(.*) ) {
set $sub_name $1;
rewrite ^/(.*)$ /index.php/$sub_name/$1 last;
break;
}
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
break;
}
}
...
}
I want to the results like:
sub1.domain.com/c1/a1 -> domain.com/sub1/c1/a1
sub2.domain.com/c2/a2 -> domain.com/sub2/c2/a2
How to correct it?
server {
server_name sub1.domain.com;
return 301 "http://domain.com/sub1${uri}";
}
Would this work for you?
I just noticed an answer to this here: https://serverfault.com/questions/426673/nginx-redirect-subdomain-to-sub-directory
server {
server_name ~^(?<sub>[a-zA-Z0-9-]+)\.domain\.com$; # will cause the sub-domain to be placed in $sub
return 301 "http://domain.com/${sub}${uri}";
}
I think the regular expression also can be a very cool solution... Depends what you need.

Nginx rewrite only when root domain

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

Resources