NginX Rewrites
server {
listen 80;
server_name www.foo.com;
rewrite ^/(.*) http://foo.com/$1 permanent;
}
to strip the www part of the domain.
I also own the domain foo.co.uk. What I want to do is have visitors to foo.co.uk redirected to foo.com. So, would the following work or do I have to brush up on my Regex:
server {
listen 80;
server_name foo.co.uk;
rewrite ^/(.*) http://foo.com/$1 permanent;
}
server {
listen 80;
server_name www.foo.co.uk;
rewrite ^/(.*) http://foo.com/$1 permanent;
}
3 Replies
^/(.*)
which means "beginning of line, followed by a / and then rember everything else as $1. So "/hello/there" would make $1="hello/there". Your redirect then rewrites that as
server {
listen 80;
server_name www.foo.com foo.co.uk www.foo.co.uk;
rewrite ^/(.*) http://foo.com/$1 permanent;
}