1

I have HTTPS redirect in nginx.conf

  1. But if the requested URI is HTTP and 404 - I don't need a redirect. Is there any solution for this specific configuration?

  2. Also is there a possibility to turn off HTTPS + www redirect for 404 links like http://www.test.com

  3. If point 1 or 2 cannot be done, can I hardcode a list of 404 URLs with nginx to completely turn off any redirect for them and only show 404 page? (HTTPS and www)

WordPress + Nginx (+Vestacp)

server {
    listen      x.x.x.x:80;
    server_name test.com www.test.com;
    root        /home/admin/web/test.com/web/;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/test.com.log combined;
    access_log  /var/log/nginx/domains/test.com.bytes bytes;
    error_log   /var/log/nginx/domains/test.com.error.log error;
    return 301 https://test.com$request_uri;
}

1 Answers1

0

Welcome to ServerFault.

But if the requested URI is HTTP and 404 - I don't need a redirect. Is there any solution for this specific configuration?

I don't know how to do this. I am unsure if it is even possible.

Also is there a possibility to turn off HTTPS + www redirect for 404 links like http://www.test.com

I don't understand this question.

If point 1 or 2 cannot be done, can I hardcode a list of 404 URLs with nginx to completely turn off any redirect for them and only show 404 page? (HTTPS and www)

This is possible. Assuming we have the list of files at /home/admin/web/test.com/web/404.conf in the following format...

/aboutt-us/ 404;
/about-uss/ 404;
/contactt-us/ 404;
/contact-uss/ 404;

Instead of the text "404", we could have anything except "0" (zero).

The modified configuration would look like this...

map $uri $allwillburnn { include /home/admin/web/test.com/web/404.conf; }

server {
    listen      x.x.x.x:80;
    server_name test.com www.test.com;
    root        /home/admin/web/test.com/web/;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/test.com.log combined;
    access_log  /var/log/nginx/domains/test.com.bytes bytes;
    error_log   /var/log/nginx/domains/test.com.error.log error;
    if ( $allwillburnn ) { return 404; }
    return 301 https://test.com$request_uri;
}

Explanation: By default, for any URLs not listed in the aforementioned 404.conf file, the variable named $allwillburnn is assigned to a value of 0 (zero) by map. In this case, if block is skipped, resulting in 301 redirect.

For URLs listed in 404.conf file, $allwillburnn is assigned to non-zero value. In this case, if block is parsed, resulting in 404.

I hope that helps.

Pothi Kalimuthu
  • 6,117
  • 2
  • 26
  • 38