nginx rewrite rules from main domain to subdomain - nginx

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

Related

Nginx rewrite does not change url in the browser

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

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

redirect only www requests to https in nginx configuration file

I have a node application running on EBS where I want to enforce https. I have overridden the nginx configuration file with the following command
server_name www.mydomain.com;
if ($http_x_forwarded_proto != "https") {
rewrite ^(.*)$ https://$server_name$REQUEST_URI permanent;
}
This works fine but I have a separate dev domain of dev.mydomain.com which I want to connect to over http rather than https.
Is there an nginx prefix variable can access to do something like...
server_name www.mydomain.com;
if ($http_x_forwarded_proto != "https" && $uri_prefix == 'www') {
rewrite ^(.*)$ https://$server_name$REQUEST_URI permanent;
}
Nginx doesn't allow multiple or nested if statements
You could try this configuration but...it's not so elegant
`
if ($http_x_forwarded_proto != "https") {
set $myvar http;
}
if ($host ~ ^www) {
set $myvar "${myvar}www";
}
if ($myvar = 'httpwww') {
rewrite ^(.*)$ https://$server_name$REQUEST_URI permanent;
}
`

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

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.

Resources