Nginx rewrite does not change url in the browser - nginx

I have nginx config like this:
server {
server_name forum.xyz.pl;
#return 301 https://forum.xyz.pl$request_uri;
#rewrite ^ xyz.pl$request_uri? permanent;
rewrite ^/(.*)$ https://xyz.pl/$1 permanent;
}
server {
...
server_name xyz.pl;
...
}
Openning forum.xyz.pl in the browsers works but then url in the browser is still old ( still forum.xyz.pl instead of expected xyz.pl ), what should I change there so the user will new url in the browser ?
full config: https://gist.github.com/maciejbak85/c9a50919b606a110bf25a6a9fd950cb2

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

How to fix redirects in nginx after the front-slash in the end of the URL

We are changing our web from example.se to example.com and also changing a few categories from Swedish to English so I need to point the old URLs to new ones with new names.
I am trying to redirect example.se/something to example.com/anything-else
I've managed to accomplish the redirection from example.se > example.com but whenever I try to go to example.com/something > example.com/anything-else I get a 404.
Here's the nginx conf:
server {
listen 80;
listen 443;
server_name example.se www.example.se;
rewrite ^/(.*)$ http://.example.com/$1 permanent;
rewrite ^/something/$ /anything-else/ permanent;
}
What I've managed to accomplish is the redirection from example.se > example.com but whenever I try to go to example.com/something (which I'd like to point to example.com/anything-else) I get a 404.
In second redirect you should specify domain name: rewrite ^/something/$ http://example.com/anything-else/ permanent instead rewrite ^/something/$ /anything-else/ permanent
Or you could use if:
if ($request_uri = / ) {
rewrite (.*) http://example.com/ permanent;
}
if ($request_uri ~ ^/something) {
rewrite (.*) http://example.com/anything-else permanent;
}

nginx rewrite rules from main domain to subdomain

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

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