Adithya
Adithya

Reputation: 163

domain to www.domain redirection

I have an nginx server behind varnish and I am trying to do a domain to www.domain redirection .

I tried using these rules on nginx

   rewrite ^(.*) http://www.domain.com$1 permanent;
   return 301 ^ $scheme://www.domain.com$request_uri;
   return 301 http://www.domain.com$request_uri;

But I get an error in chrome as website running into a redirection loop.

As the about solution did not work I tried an alternative writing rules in varnish

sub vcl_recv {
  // ...
  if ( req.http.host == "domain.com" ) {
    error 750 "http://www." + req.http.host + req.url;
  }
  // ...
}

sub vcl_error {
  // ...
  if (obj.status == 750) {
    set obj.http.Location = obj.response;
    # Set HTTP 301 for permanent redirect
    set obj.status = 301;
    return(deliver);
  }
  // ...
}

I am using varnish 4 and I get an error that varnish can't compile the code.

Message from VCC-compiler:
Expected an action, 'if', '{' or '}'
('input' Line 29 Pos 3)
  error 750 regsub(req.http.host, "^www\.(.*)", "http://\1"); 
--#####------------------------------------------------------------------------------------------------------------------------

Could some one please help me in fixing this?

My server block is a follows:

server {
        listen  127.0.0.1:8080;
        root /home/webadmin/html/livesite;
        index index.php index.html index.htm;

        server_name www.domain.com;
#       rewrite ^(.*) http://www.domain.com$1 permanent;
#       return 301 ^ $scheme://www.domain.com$request_uri;
#       return 301 http://www.domain.com$request_uri;     
        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        error_page 404 403 /error/eror.html;
        location = /error/error.html {
                root /home/webadmin/html/livesite;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
             root /home/webadmin/html/livesite;
        }
        #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9$
        location ~ \.php$ {
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;

                 }
}

Upvotes: 0

Views: 868

Answers (2)

Daniel Asplund
Daniel Asplund

Reputation: 11

Use varnish for this instead, and skip the apache rewrites. This example is to always redirect to https, but the semantics is the same..

In sub vcl_recv:

if ( req.http.host ~ "^(?i)domain.com" )
{
    set req.http.X-Redir-Url = "https://domain.com" + req.url;
    error 750 req.http.x-Redir-Url;
}

and then in sub vcl_error:

if (obj.status == 750) {
    set obj.http.Location = obj.response;
    set obj.status = 302;
    return (deliver);
}

Upvotes: 0

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42899

This would be a correct way to do it while preserving the uri and the query string,

return 301 $scheme://www.domain.com$request_uri$is_args$query_string;

The problem isn't in this part, the problem is where you are redirecting to, probably redirecting to another locations that also does another redirect, I would guess you don't have another server block to handle the www server separately, so you keep redirecting to the same place over and over, wouldn't know for sure till you post the rest of the config.

EDIT:

The issue like I said is that you are redirecting the www to the www server, to avoid that you should create a new server that isn't with the www to do the redirection

server { # the redirecting server
  listen 8080; # according to your config
  server_name domain.com; #without www
  return 301 $scheme://www.domain.com$request_uri$is_args$query_string;
}
server { # the actual serving server
  listen 8080;
  server_name www.domain.com;
  # the rest of your actual settings
}

Upvotes: 1

Related Questions