Nginx Redirect www to non-www (NRWN)


NRWN:   SOLD       




Hi dev, sorry the site was down for a few minutes because I was doing maintenance. What do you think is the reason I did this?

Okay honestly, the past 3 days I have been very busy in the real world, so I haven't had time to touch this site even for a moment. Without realizing it, the weekend has arrived, and luckily I have some free time to visit this beloved forum.

So, as usual, I checked the keywords on Google about this forum, and it turned out that my root domain index position changed to have  www -. Then I tried to open it, it turned out that the site was not running properly because the default for  base_url this forum is without  www.

Okay, I immediately  GET  my Google-fu to solve this problem. It took some trial and error, but finally after reading more than 3 references I finally understood.

Conclusion

To perform a permanent redirect (301) via Nginx config, we must not put the lines of code in  this server block  into   another existing server block , that will cause an error;

ERR_TOO_MANY_REDIRECTS

So, to avoid this error, just put this one block of code at the bottom of your nginx config file, here's how;

Point your terminal to this location  /etc/nginx/sites-available and open the associated site's virtual host configuration using nano.

COPY, block the code below and put it on the bottom line outside the existing server block. Don't forget to replace it  example with your domain name.

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

Or slightly modify the server block resulting from the certbot installation.

server {
    if ($host = www.bundet.com) {
        return 301 https://bundet.com$request_uri;  #<== modif bagian ini!
    } # managed by Certbot


    if ($host = bundet.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name bundet.com www.bundet.com;
    return 404; # managed by Certbot
}

The server block above only applies to the http protocol, but my letsencrypt has generated a configuration that also redirects http to https, that's why we only need to create a redirect server block for port 80 (http protocol path).

Ok, just save it, restart the nginx service, done!

Extra Configuration

To optimize the main goal, which is that I don't want my www to be indexed by Google, then I need to tell the Google spider not to crawl my site carelessly, so I add the sitemap path in the  robots.txt file .

User-agent: Googlebot
Disallow:
User-agent: MJ12bot
Disallow:
User-agent: msnbot
Disallow:
User-agent: Bingbot
Disallow:
User-agent: Yahoo-slurp
Disallow:
User-agent: Slurp
Disallow:
User-agent: Mediapartners-Google
Disallow:
User-agent: Googlebot-Mobile
Disallow:
User-agent: AdsBot-Google
Disallow:
User-agent: Yahoo-MMCrawler
Disallow:
User-agent: Twitterbot
Disallow:
User-agent: Facebot
Disallow:

User-agent: *
Disallow: /

Sitemap: https://bundet.com/sitemap.xml

In addition, I also did it for the  .htaccess file configuration.

# Redirect Pages www to root
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Warning!

If you have done it correctly, according to this tutorial, but when you test it in any browser it still doesn't work, then  DON'T PANIC! , because it's just a problem with your browser's cache & history. So please neutralize your browser first before testing it. If you are not sure about that action, please use your cellphone browser, or download Tor Browser to test it!

Redirect Subdomain to Root Domain via htaccess

Let's say I have a domain bundet.com & have a subdomain sig.bundet.com

Let's say my subdomain has had a lot of flying hours in the cloud since 2001, so there are lots of links scattered out there.

Once I intended to close the subdomain for some reason, without wanting to lose traffic from the links that have been spread in various corners of the cyber world. So I need to redirect the subdomain to the bundet.com domain

So is there the best way to handle my idea?

Completion

Of course you can, if in this case you do not delete all pages owned by the subdomain, then you can do it with javascript by placing this code on each page owned by the subdomain, or together with the content, or it can also be placed in the header / footer. We will review this method in more detail on another page.

Back to the main topic, so there is a best way to do it, which is using .htaccess 

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} subdomain.domain.com
    RewriteRule /(.*) http://domain.com [R=301,L]

</IfModule>

Good luck!


Post a Comment

Previous Next

نموذج الاتصال