
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
In today's fast-moving website competition SEO is the game-changer of your website. One of the problem what an SEO faces is multiple links to your domain, for example, www.stackcoder.in and stackcoder.in ie redirect www or a non-www website or vice versa.
If your looking out for LEMP installation then please do check my another article on it How To Install Linux, NGINX, MYSQL, PHP (LEMP Stack) on Ubuntu
The major impact for SEO is when user may come from www or non-www link of your website. And if your web server is not capable to handle those and start serving on the same requested links, then your SEO score is totally divided between your 2 domains links.
NGINX
web serverYou need to redirect all your users website URL
requests from your web server ( NGINX
or APACHE
) to single point those ie either to www.stackcoder.in or stackcoder.in with 301
HTTP status redirect.
301
redirect is a permanent redirect which redirects users & search engines like Google, Yahoo, DuckDuck Go, etc. But we need to tell them where does this redirect to.
If you like to learn NGINX
caching then I have already written article on it How To Cache Static Files With NGINX Server.
Go to your website NGINX server configuration which will be mostly in /etc/nginx/sites-available/* and mine is /etc/nginx/sites-available/stackcoder.in which will basically have the following configuration. I have striped few code lines but structure remains the same for you guys
server {
listen 443 ssl http2;
root /var/www/html/public;
index index.php index.html;
server_name stackcoder.in www.stackcoder.in;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
#SSL CERTIFICATE FILES
}
server {
listen 80;
server_name stackcoder.in www.stackcoder.in;
return 301 https://$host$request_uri;
}
In the following configuration file NGINX
am listening on 443
port on HTTPS
for website URL
www.stackcoder.in and redirecting to permanently with 301
HTTP
status code to non-www
website ie stackcoder.in
server {
listen 443 ssl http2;
#Listen on https for https://www.stackcoder.in
server_name www.stackcoder.in;
#Redirect to non www URL ie https://stackcoderin
return 301 https://stackcoder.in$request_uri;
}
With the above change, your overall NGINX configuration file must look like the following
server {
root /var/www/html/public;
index index.php index.html;
server_name stackcoder.in;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
listen 443 ssl http2;
#SSL CERTIFICATE FILES
}
server {
listen 443 ssl http2;
server_name www.stackcoder.in;
return 301 https://stackcoder.in$request_uri;
}
server {
listen 80;
server_name stackcoder.in www.stackcoder.in;
return 301 https://$host$request_uri;
}
This is similar to Option 1 with minor change in the configuration syntax
Here NGINX
is listening on 443
port on HTTPS
for website URL
stackcoder.in and redirecting to permanently with 301
HTTP
status code to non-www
website ie www.stackcoder.in
server {
listen 443 ssl http2;
#Listen on https for https://www.stackcoder.in
server_name stackcoder.in;
#Redirect to non www URL ie https://stackcoderin
return 301 https://www.stackcoder.in$request_uri;
}
With the above change, your overall NGINX configuration file must look like the following
server {
root /var/www/html/public;
index index.php index.html;
server_name www.stackcoder.in;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
listen 443 ssl http2;
#SSL CERTIFICATE FILES
}
server {
listen 443 ssl http2;
server_name stackcoder.in;
return 301 https://www.stackcoder.in$request_uri;
}
server {
listen 80;
server_name stackcoder.in www.stackcoder.in;
return 301 https://$host$request_uri;
}
NGINX
serverOnce you opt for either Option 1 or Option 2 make sure to restart your NGINX
web server to take the changes into effect. Else the configuration that you have added wont work.
First check if the NGINX
added code to configuration file is proper or not by the following command
sudo nginx -t
You must be able to see similar output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Now reload the NGINX
server configuration file with the following
sudo service nginx reload
Now you have learned how to setup NGINX redirects. I have written other articles which might be very interesting for you How To Install Linux, NGINX, MYSQL, PHP (LEMP Stack) on Ubuntu where you will learn about step by step installation of LEMP stack.
If your looking our to securing you server then make sure not to miss out this tutorial How To Do Basic Server Security Setup For Ubuntu / Linux
Generate Fake Data In PHP With Faker
Install Packages Parallel For Faster Development In Composer
Integrate Google Translate Into Your Website
Route Model Binding In Laravel & Change Default Column id To Another Column
Securely SSH Your Server & Push Files With FileZilla
Install Letsencrypt SSL Certificate for RabbitMQ Server and RabbitMQ Management Tool
Debugging Laravel Queue Email Issues
Global Data In All Laravel Blade Pages
Google reCAPTCHA Integration In PHP Laravel Forms
Dependency Dropdowns With Javascript And PHP
Search Engine Optimization Concepts
Why namespace And use Keywords Used In PHP
Accessors And Mutators In PHP Laravel
Laravel 7.x Multiple Database Connections, Migrations, Relationships & Querying