Regular Expression global redirect

Mark Alan varia at e-healthexpert.org
Sun Feb 26 12:18:41 UTC 2012


On Sat, 25 Feb 2012 22:59:50 -0500 (EST), "altiamge"
<nginx-forum at nginx.us> wrote:

> I'm using nginx as a reverse proxy for about 2000 websites. I'm trying
> to find a good way to redirect all www traffic to nonwww addresses.
> here's my server block:
> 
> server {
>  	listen 80;
>  	server_name	_;
> 
>  	location / {
>  		proxy_pass  http://websites;
>  	}
> }

Would this help?

For older PCRE's:

# for http
server { listen 80; server_name ~^www\.(?P<domain>.+)$; return 301
$scheme://$domain$request_uri; }

#for https (change 'sslcert' for your own certificate name)
server { listen 443 ssl; server_name ~^www\.(?P<domain>.+)$;
ssl_certificate /etc/ssl/certs/sslcert.crt;
ssl_certificate_key /etc/ssl/private/sslcert.key; return 301
$scheme://$domain$request_uri; }

For newer PCRE's:
Instead of ?P<domain> use ?<domain>

# Note: in 'return XXX' 301 is like rewrite...permanent and
302 like rewrite...redirect

M.



More information about the nginx mailing list