rewrite non-www to www

Fred.

Verified User
Joined
Sep 5, 2009
Messages
220
Hi,

Can anyone help me with this?
I'm trying to rewrite non-www to www

I already have this in the nginx.conf Customization (custom 3) box to rewrite http to https

|*if SSL_TEMPLATE="0"|
rewrite ^(.*)$ https://www.example.com$1 permanent;
|*endif|

But now I also want to rewrite https://example.com to https://www.example.com

I tried some different configurations, but no success so far. :confused:
 
Hello Fred,

To accomplish the desired in NGINX you should split a virtual host
Code:
server {
...
server_name  www.domain.com domain.com;

...

into two separate

Code:
server {
...
server_name  domain.com;
return 301 https://www.domain.com$request_uri;
...

and

Code:
server {
...
server_name  www.domain.com;

...


Related: http://nginx.org/en/docs/http/converting_rewrite_rules.html
 
Hi Alex,

I found and tried that code before.

So I have to edit files for that?

I tried to put it in Custom 3

Code:
Unable to write customization

Details
nginx: [emerg] "server" directive is not allowed here in /usr/local/directadmin/data/users/admin/nginx.conf:437
nginx: configuration file /etc/nginx/nginx.conf test failed
 
I changed
Code:
|CUSTOM|

	listen |IP|:|PORT_443| ssl;
	|MULTI_IP|

	server_name |DOMAIN| www.|DOMAIN| |SERVER_ALIASES|;
to
Code:
|CUSTOM|

	listen |IP|:|PORT_443| ssl;
	|MULTI_IP|

	server_name |DOMAIN| |DOMAIN| |SERVER_ALIASES|;
	return 301 https://www.|DOMAIN|$request_uri;

	server_name |DOMAIN| www.|DOMAIN| |SERVER_ALIASES|;

Does that look ok?
I'll try it tonight. The server is too busy now.
 
I'm editing /usr/local/directadmin/data/templates/custom/nginx_server_secure.conf

But if I don't use the |DOMAIN| token it's going to rewrite all my domains on the server to that domain?

Sorry, I still not familiar with the nginx configuration. There are so many configuration files.
 
Ok, Thanks Alex.
It looks like I have to test it another day. Website still too busy. I'm going to sleep now.
 
I tried at least 10 different configurations. None of them was working. :confused:
Nginx isn't starting.

This is what I have now in /usr/local/directadmin/data/templates/custom/nginx_server_secure.conf
This is working, but not rewriting to www.

Code:
|CUSTOM1|
|?DOCROOT=`HOME`/domains/`DOMAIN`/private_html|
|?REALDOCROOT=`HOME`/domains/`DOMAIN`/private_html|
|?OPEN_BASEDIR_PATH=`HOME`/:/tmp:/var/tmp:/usr/local/lib/php/|
server
{
|CUSTOM|

	listen |IP|:|PORT_443| ssl;
	|MULTI_IP|

	server_name |DOMAIN| www.|DOMAIN| |SERVER_ALIASES|;

	access_log /var/log/nginx/domains/|DOMAIN|.log;
	access_log /var/log/nginx/domains/|DOMAIN|.bytes bytes;
	error_log /var/log/nginx/domains/|DOMAIN|.error.log;

	root |DOCROOT|;

	index index.php index.html index.htm;

	ssl on;
	ssl_certificate |CERT|;
	ssl_certificate_key |KEY|;

	ssl_session_cache   shared:SSL:10m;
	ssl_session_timeout 5m;

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_ciphers HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers on;

	|NGINX_PHP_CONF|
	
|*if HAVE_NGINX_PROXY="1"|
	location /
	{
|CUSTOM2|
|LOCATION_INSERT|
		# access_log off;
		proxy_buffering |PROXY_BUFFERING|;
		proxy_pass https://|IP|:|PORT_8081|;
		proxy_set_header X-Client-IP      $remote_addr;
		proxy_set_header X-Accel-Internal /nginx_static_files;
		proxy_set_header Host             $host;
		proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
	}
	location /nginx_static_files/
	{
		# access_log  /var/log/nginx/access_log_proxy;
		alias       |DOCROOT|/;
		internal;
	}
|*else|
|NGINX_REDIRECTS|
|PROTECTED_DIRECTORIES|
|EXTRA_LOCATIONS|
|*endif|

|CUSTOM3|

	include /etc/nginx/webapps.ssl.conf;
	
|CUSTOM4|
}
 
I finally got it working

Code:
|CUSTOM1|
|?DOCROOT=`HOME`/domains/`DOMAIN`/private_html|
|?REALDOCROOT=`HOME`/domains/`DOMAIN`/private_html|
|?OPEN_BASEDIR_PATH=`HOME`/:/tmp:/var/tmp:/usr/local/lib/php/|
server
{
|CUSTOM|

	listen |IP|:|PORT_443| ssl;
	|MULTI_IP|

	server_name  |DOMAIN|;
	return       301 http://www.|DOMAIN|$request_uri;
}
server
{
	listen |IP|:|PORT_443| ssl;
	|MULTI_IP|

	server_name |DOMAIN| www.|DOMAIN| |SERVER_ALIASES|;

	access_log /var/log/nginx/domains/|DOMAIN|.log;
	access_log /var/log/nginx/domains/|DOMAIN|.bytes bytes;
	error_log /var/log/nginx/domains/|DOMAIN|.error.log;

	root |DOCROOT|;

	index index.php index.html index.htm;

	ssl on;
	ssl_certificate |CERT|;
	ssl_certificate_key |KEY|;

	ssl_session_cache   shared:SSL:10m;
	ssl_session_timeout 5m;

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_ciphers HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers on;

	|NGINX_PHP_CONF|
	
|*if HAVE_NGINX_PROXY="1"|
	location /
	{
|CUSTOM2|
|LOCATION_INSERT|
		# access_log off;
		proxy_buffering |PROXY_BUFFERING|;
		proxy_pass https://|IP|:|PORT_8081|;
		proxy_set_header X-Client-IP      $remote_addr;
		proxy_set_header X-Accel-Internal /nginx_static_files;
		proxy_set_header Host             $host;
		proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
	}
	location /nginx_static_files/
	{
		# access_log  /var/log/nginx/access_log_proxy;
		alias       |DOCROOT|/;
		internal;
	}
|*else|
|NGINX_REDIRECTS|
|PROTECTED_DIRECTORIES|
|EXTRA_LOCATIONS|
|*endif|

|CUSTOM3|

	include /etc/nginx/webapps.ssl.conf;
	
|CUSTOM4|
}
 
All the domains with SSL activated on my server seems to rewrite https://example.com to https://www.example.com
That was not really what I wanted. I just wanted that for 1 domain.

Now on another domain I would like to rewrite https://www.example.com to https://example.com

So I guess I have to put the old code back in /usr/local/directadmin/data/templates/custom/nginx_server_secure.conf

And then put
Code:
rewrite ^(.*)$ https://example.com$1 permanent;
in CUSTOM3 ?

Just want to be sure before I start messing up things. :p
 
I got this fixed to :)

This is what I have in custom http (the first box)
this is non-www to www (https)

Code:
|*if DOMAIN="example.com"|
	listen |IP|:|PORT_80|;
	|MULTI_IP|

	server_name  www.|DOMAIN|;
	return       301 https://www.|DOMAIN|$request_uri;
}
server
{
	listen |IP|:|PORT_80|;
	|MULTI_IP|

	server_name  |DOMAIN|;
	return       301 https://www.|DOMAIN|$request_uri;
}
server
{
	listen |IP|:|PORT_443| ssl;
	|MULTI_IP|

	server_name  |DOMAIN|;
	return       301 https://www.|DOMAIN|$request_uri;
}
server
{
|*endif|
 
Last edited:
For anyone interested (since this page ranks high in google about this) here is what conveniently works for me without changes inside template files on such situations.


Below is what I enter in "Custom HTTPD Configurations", in the top textarea input.


In order to redirect (301) all non-www to www:

Code:
#redirect non-www to www
if ($host ~* ^|DOMAIN|$) {
    return 301 $scheme://www.|DOMAIN|$request_uri;
}


in order to redirect (301) all non-SSL to www-SSL I put the following first:

Code:
|*if SSL_TEMPLATE="0"|
        return 301 https://www.|DOMAIN|$request_uri;
|*endif|
 
Back
Top