Skip to content Skip to sidebar Skip to footer

Doing Website Redirect With NGinx

Doing Website Redirect With NGinx

It's lightning fast, runs of the tiniest of servers and can handle a tsunami of traffic.

So what is a website redirect?

It's simply sends web traffic from one URL to another URL, without the user having to click any links in the middle.

So lets say we want to redirect all traffic going to our old website (lets call it oldsite.com) to our new website (lets call its newsite.com), we would setup a redirect on oldsite.com to send all the traffic automatically to newsite.com



There are two main types of website redirects (in SEO terms).

There is a permanent redirect (commonly called the 301 redirect). This tells the search engines and more modern web browsers that whenever we see this URL automatically treat it as a different URL

So in out example above, if we put a 301 redirect on oldsite.com, the web browsers and search engines would simply substitute oldsite.com with newsite.com and basically act as if oldsite.com does not exists anymore.

A temporary or 302 redirect, says to the web browsers and search engines that the link destination may change, so don't automatically do a substitution.

Let's says you have a company blog and you set up a redirect to the latest news article.

So something like companysite.com/latest_news is a redirect to complanysite.com/news/2021-07-03/article.HTML (2021-07-03 is the date of writing). I want this latest_news to send people to the news article, but when a newer article appears, I want it to redirect tp the newer article instead.

So let's make these redirect using nGinx, using the example given above.

Some where near the start of your conf file in your /etc/nginx/conf.d/ folder, put the following line:

server {

listen 443 ssl http2;

server_name companysite.com;

root /website;

index index.HTML;

ssl_certificate... ;

ssl_certificate_key... ;

# This will create a 302 link

rewrite ^/latest_news$ /news/2021-07-03/article.HTML redirect;

# This will create a 301 link

rewrite ^/latest_news$ /news/2021-07-03/article.HTML permanent;

...

}

So you can see that creating nGinx redirect links is pretty simple and the best part is you don't have to write any code, install any complicated plugins or mess about with databases.

Post a Comment for "Doing Website Redirect With NGinx"