WordPress.org

Ready to get started?Download WordPress

Forums

Nginx and Redirects (6 posts)

  1. jdubbin
    Member
    Posted 1 year ago #

    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!

  2. Yui
    Member
    Posted 1 year ago #

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

    additionally i recommend (for SEO issues)
    adding

    Host: www.site.com

    to /robots.txt

  4. jdubbin
    Member
    Posted 1 year ago #

    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.

  5. Yui
    Member
    Posted 1 year ago #

    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.

  6. Pothi
    Member
    Posted 1 year ago #

    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;

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.