• Hello,

    I’m fairly new to nginx, I have the latest version and I’m running it on centos 6. WordPress is currently installed on the root of my domain.

    Everything is working perfectly, even got permalinks working just fine.

    My problem is, whenever I click my link from a Google search, it will go to my root domain. It took me a minute but I figured out why.

    Google is displaying mysite.com/specific-post and I have a 301 setup for non-www to go to www.

    I believe I need to fix the redirect so it not only redirects to www, but to the http://www.site.com/specif-post I’m not sure if in the redirect its /$ for variable or what. Here is my current code.

    server {
            listen 80;
            server_name site.com;
            rewrite     ^ http://www.site.com  permanent;
    }
    
    server {
            listen 80;
            server_name www.site.com;
    
                    root   /usr/local/nginx/html/site.com;
                    index index.php;
                    include /usr/local/nginx/custom/gzip.conf;
                    include /usr/local/nginx/custom/php.conf;
    
    location / {
    try_files $uri $uri/ /index.php;
          }
    }

    If someone could please tell me the redirect variable to get it to pass on to the redirected URL that would be awesome!

    Thanks in advanced!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Yui

    (@fierevere)

    永子

    server {
            listen 80;
            server_name www.site.com site.com;
    location / {
    if ( $host != 'www.site.com' ) { rewrite  ^/(.*)$  http://www.site.com/$1 permanent; }
    Moderator Yui

    (@fierevere)

    永子

    additionally i recommend (for SEO issues)
    adding

    Host: www.site.com

    to /robots.txt

    Thread Starter jdubbin

    (@jdubbin)

    Awesome! Thanks, it worked perfectly.

    Regarding the robots.txt

    Can I simply create a robots.txt in my directory or do I have to add it to my nginx config somehow?

    Thanks again, you really helped me out.

    Moderator Yui

    (@fierevere)

    永子

    just create a file

    User-agent: *
    Crawl-delay: 2
    Disallow: /wp-includes/
    Disallow: /wp-admin/
    Disallow: /wp-content/plugins
    Disallow: /wp-content/themes
    Disallow: /search
    
    Host: www.site.com

    something like this. Sitemap recommended too 🙂 but you’ll need a plugin to generate/update it.

    Alternative solution…

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

    Let me quote what Nginx wiki says about if statement…

    The only 100% safe things which may be done inside if in location context are:

    return …;
    rewrite … last;

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Nginx and Redirects’ is closed to new replies.