Question about redirecting example for forcing a host name

Igor Sysoev is at rambler-co.ru
Tue Aug 28 20:20:48 MSD 2007


On Tue, Aug 28, 2007 at 11:12:47AM -0500, Fred Palmer wrote:

> I found an Nginx example for forcing the host name and wanted to know if
> this is the best way to do it.  Basically I have several domains, some are
> misspellings of our primary domain and we want them to all go to our primary
> domain.  So I found this example:
> 
> server {
>   listen 80;
>   server_name example.com www.example.com;
> 
>   if ($host != 'example.com' ) {
>       rewrite  ^/(.*)$  http://example.com/$1  permanent;
>   }
> }
> 
> 
> Is this the best way?

No. You should route requests using "server", but not "if".

   server {
       listen  80;
       server_name   example.com;
       ...
   }

   # note "default", this server will receive all responses to 80 port,
   # that does not match "example.com".

   server {
       listen  80    default;
       server_name   www.example.com;
       rewrite       ...
   }


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list