You have a website say example.com, you want it only to be accessed by www.example.com, instead of both example.com & www.example.com or you may want your domain to be only accessed by example.com instead of both example.com & www.example.com.
The domain with or without www does'nt effects search engine optimization (SEO).
1. Redirect your website from non-www to www i.e. example.com to www.example.com
Configure your nginx server in this way:
Just add this at starting of your site config file which is generally located at: /etc/nginx/sites-enabled/yourdomain.com
server {
listen 80;
server_name yourdomain.com;
return 301 http://www.yourdomain.com$request_uri;
}
Change `yourdomain.com` with your domain.
This code returns a 301 Redirect to the clients browser and forces to load content from `www` version of your website
If you are using apache2 as your server add the following content to .htaccess file on root of your project. We will use Rewrite Engine Module of apache2 for doing this.
Note: if you don't have .htaccess file, create one!
2. Redirect your website from www to non-www i.e. www.example.com to example.com
Configure your nginx server in this way:
Just add this at starting of your site config file which is generally located at: /etc/nginx/sites-enabled/yourdomain.com
server {
listen 80;
server_name www.yourdomain.com;
return 301 http://yourdomain.com$request_uri;
}
What is www and why should I redirect to my preferred domain?
Well here's the answer: Www is actually a subdomain of your domain. www actually refers to [world wide web] hmm! well than why redirect? The best answer is both the domains might have same content i.e. yourdomain.com and www.yourdomain.com, and its not very good to the search engine bots.What should I choose? www or non-www domain?
This is completly depended upon you and the name of your domain. Sometimes domains are quite good without www for example https://letsencrypt.org.The domain with or without www does'nt effects search engine optimization (SEO).
1. Redirect your website from non-www to www i.e. example.com to www.example.com
Configure your nginx server in this way:
Just add this at starting of your site config file which is generally located at: /etc/nginx/sites-enabled/yourdomain.com
server {
listen 80;
server_name yourdomain.com;
return 301 http://www.yourdomain.com$request_uri;
}
Change `yourdomain.com` with your domain.
This code returns a 301 Redirect to the clients browser and forces to load content from `www` version of your website
If you are using apache2 as your server add the following content to .htaccess file on root of your project. We will use Rewrite Engine Module of apache2 for doing this.
Note: if you don't have .htaccess file, create one!
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
2. Redirect your website from www to non-www i.e. www.example.com to example.com
Configure your nginx server in this way:
Just add this at starting of your site config file which is generally located at: /etc/nginx/sites-enabled/yourdomain.com
server {
listen 80;
server_name www.yourdomain.com;
return 301 http://yourdomain.com$request_uri;
}
Change `yourdomain.com` with your domain.
This configuration will make the client redirect to non-www version of website.
For apache2 add this to the .htaccess file in the root of your project/website folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Thank you for reading this article hope you liked it. Will be back soon with another article.
Comments
Post a Comment