Nginx HTTP & HTTPS Configs: Redirect SOME URLs

I have just relaunched a WordPress site on Nginx, serving content over HTTP. At present we are not looking to migrate the entire site to HTTPS, however the old version of the site had pages at https://store.website.com.

Now the new versions of those old pages (for instance, https://store.website.com/category/this-product.html, or, https://store.website.com/that-product.html) live at http://www.website.com/store/category/this-product/.

Read More

We have lots of links in about 10,000 posts to the HTTPS pages, so for us right now instead of changing all of those links in our posts, it makes the most sense to simply redirect those https://store.... pages to the new http page.

I have a full list of the URLs of the old HTTPS pages that we want to redirect. We are still loading a number of products in to the new website (there are about 100 there), and still need to load in another 900 or so. Therefore, we will be using 301 redirects for some URLs (the products already loaded), and 302 redirects to the top level category page for the remainder of the products that still need to be loaded. Once those new products are loaded, we will change the 302 redirect from the top-level category page, to be a 301 redirect to the permanent home.

I have only ever setup HTTP servers in Nginx, so here are my questions:

  1. I am not sure if creating an HTTPS server will create a duplicate content for each post or not, by making the content available via two protocols
  2. How close am I with the config below to accomplishing what I have described? Note: below you will first find my HTTP Nginx config, and below the HTTPS Nginx config.

*******HTTP NGINX CONFIG*******

    server {
            listen   80;

            access_log   /var/log/nginx/access.log;
            error_log    /var/log/nginx/error.log crit;

            root /var/www;
            index index.php;

            server_name website.com www.website.com;

            client_max_body_size 100M;

            location / {
                    try_files $uri $uri/ /index.php?q=$uri&$args;
                    #auth_basic "Admin Login";
                    #auth_basic_user_file /etc/nginx/pma_pass;
            }

            error_page 404 /404.html;

            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            location ~ .php$ {
                    try_files $uri =404;
                    fastcgi_pass 127.0.0.1:9000;
                    # With php5-fpm:
                    #fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
                    fastcgi_read_timeout 60;
            }
            # BEGIN W3TC CDN
            location ~ .(ttf|ttc|otf|eot|woff|font.css)$ {
               add_header Access-Control-Allow-Origin "*";
            }
            # END W3TC CDN
            #Yoast sitemap
            location ~ ([^/]*)sitemap(.*).x(m|s)l$ {
                    rewrite ^/sitemap.xml$ /sitemap_index.xml permanent;
                    rewrite ^/([a-z]+)?-?sitemap.xsl$ /index.php?xsl=$1 last;
                    rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
                    rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
                    access_log off;
            }
            # Example of the style of single-page redirects we are currently using
            location = /this-page/ { return 301 /store/category/that-page/; }
    }

*******HTTPS NGINX CONFIG*******

    server {
           listen 443;
           server_name store.website.com;

           root /var/www;
           index index.php;

           ssl on;
           ssl_certificate /etc/nginx/ssl/server.crt;
           ssl_certificate_key /etc/nginx/ssl/server.key;

           ssl_session_timeout 10m;

           ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
           ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
           ssl_prefer_server_ciphers on;

           location / {
                   try_files $uri $uri/ =404;
           }
    }

Thanks for your input!

Related posts

Leave a Reply

1 comment

  1. You can consider creating a new config that listens to your old site URL and create a rewrite rule to redirect to the new site.

    http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

    UPDATE 1:

    server {
           listen 443;
           server_name store.website.com;
    
           ssl on;
           ssl_certificate /etc/nginx/ssl/server.crt;
           ssl_certificate_key /etc/nginx/ssl/server.key;
    
           ssl_session_timeout 10m;
    
           ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
           ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
           ssl_prefer_server_ciphers on;
    
           # To rewrite URLs like https://store.website.com/category/this-product.html
           rewrite ^/category/(.*).html$  http://www.website.com/store/category/$1 permanent;
    
           # To rewrite URLs like https://store.website.com/that-product.html
           rewrite ^/(.*).html$  http://www.website.com/store/category/$1 permanent;
    
           # To rewrite URLs like https://store.website.com/abcasc/q343f/3f3/f3
           rewrite ^(.*) http://www.website.com$1 permanent; 
    }
    

    Not tested. But feel free to experiment.