Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6747885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:29:58+00:00 2026-05-26T12:29:58+00:00

I am using nginx on Rackspace cloud following a tutorial and having searched the

  • 0

I am using nginx on Rackspace cloud following a tutorial and having searched the net and so far can’t get this sorted.

I want www.mysite.example to go to mysite.example as normal in .htaccess for SEO and other reasons.

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

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.example 192.192.6.8 A type at 300 seconds
www.site.example 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:29:58+00:00Added an answer on May 26, 2026 at 12:29 pm

    HTTP Solution

    From the documentation, "the right way is to define a separate server for example.org":

    server {
        listen       80;
        server_name  example.com;
        return       301 http://www.example.com$request_uri;
    }
    
    server {
        listen       80;
        server_name  www.example.com;
        ...
    }
    

    HTTPS Solution

    For those who want a solution including https://…

    server {
            listen 80;
            server_name www.domain.example;
            # $scheme will get the http protocol
            # and 301 is best practice for tablet, phone, desktop and seo
            return 301 $scheme://domain.example$request_uri;
    }
    
    server {
            listen 80;
            server_name domain.example;
            # here goes the rest of your config file
            # example
            location / {
    
                rewrite ^/cp/login?$ /cp/login.php last;
                # etc etc...
    
            }
    }
    

    Note: I have not originally included https:// in my solution since we use loadbalancers and our https:// server is a high-traffic SSL payment server: we do not mix https:// and http://.


    To check the Nginx version, use nginx -v.

    Strip www from URL with Nginx redirect

    server {
        server_name  www.domain.example;
        rewrite ^(.*) http://domain.example$1 permanent;
    }
    
    server {
        server_name  domain.example;
        #The rest of your configuration goes here#
    }
    

    So you need to have TWO server codes.

    Add the www to the URL with Nginx redirect

    If what you need is the opposite, to redirect from domain.example to www.domain.example, you can use this:

    server {
        server_name  domain.example;
        rewrite ^(.*) http://www.domain.example$1 permanent;
    }
    
    server {
        server_name  www.domain.example;
        #The rest of your configuration goes here#
    }
    

    As you can imagine, this is just the opposite and works the same way the first example. This way, you don’t get SEO marks down, as it is complete perm redirect and move. The no WWW is forced and the directory shown!

    Some of my code shown below for a better view:

    server {
        server_name  www.google.com;
        rewrite ^(.*) http://google.com$1 permanent;
    }
    server {
           listen 80;
           server_name google.com;
           index index.php index.html;
           ####
           # now pull the site from one directory #
           root /var/www/www.google.com/web;
           # done #
           location = /favicon.ico {
                    log_not_found off;
                    access_log off;
           }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Rackspace Cloud Servers. I have installed NGINX with PHP and Memcache. When
This is my first time using nginx and I'm having some problems configuring an
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
Reading this article on nginx website, I'm interested in using X-Accel-Redirect header in the
I'm using nginx with this script as FastCGI wrapper, that I start with: spawn-fcgi
I want to block all non-US users from accessing my website. I'm using nginx
How can I redirect mydomain.example and any subdomain *.mydomain.example to www.adifferentdomain.example using Nginx?
Using nginx, I want to preserve the url, but actually load the same page
We're using Nginx on the server reserved for development (testing) and we want to
I've got a problem accessing futon interface when it is proxied using nginx. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.