Welcome! Log In Create A New Profile

Advanced

Nginx no-www to www and www to no-www

Posted by khaleelm 
Nginx no-www to www and www to no-www
October 31, 2011 07:25AM
I am using nginx on Rackspace cloud following a tutorial (http://www.howtoforge.com/running-phpmyadmin-on-nginx-lemp-on-debian-squeeze-ubuntu-11.04), searched the net and so far can't get this sorted.

I want www.mysite.com to go to mysite.com as normal in .htaccess for seo and other reasons.

My vi /etc/nginx/sites-available/www.example.com.vhost code:

server {
listen 80;
server_name www.example.com example.com;
root /var/www/www.example.com/web;

if ($http_host != "www.example.com") {
rewrite ^ http://example.com$request_uri permanent;
}

I have also tried

server {
listen 80;
server_name example.com;
root /var/www/www.example.com/web;

if ($http_host != "www.example.com") {
rewrite ^ http://example.com$request_uri permanent;
}

I also tried. Both the second attempts give redirect loop errors.

if ($host = 'www.example.com' ) {
rewrite ^ http://example.com$uri permanent;
}

My DNS is setup as standard:

site.com 192.192.6.8 A type at 300 seconds www.site.com 192.192.6.8 A type at 300 seconds

(example IPs and folders have been used for examples and to help people in future). I use Ubuntu 11.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 205
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready