how to redirect non-www url to www for wordpress in Nginx and Apache?

share on:
How to redirect non-www url to www for wordpress in Nginx

Modern Search engines consider URLs with and without “www” as two different websites. It creates a duplicate entry, which is bad for SEO of your website/blog.

In this article we will learn how to redirect everything (all the http requests to https and move all the non-www requests) to https://www.example.com and vice versa in both Nginx and Apache Web Server.

If you are using WordPress then you should also change your Website Address and Site Address in General Settings page to point to right address.

I have used these settings on my server and they are working perfectly.

 

Redirecting URL’s in Nginx.

To redirect all http and https non-www  requests to https://www.example.com you should use a config similar to this in Nginx.

 

Redirect Non-www to www

server {
 listen 80;
 server_name www.example.com example.com;

# Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
 return 302 https://www.example.com$request_uri;
}

server {
 listen 443;
 server_name example.com;
 include /etc/nginx/conf-available/ssl.conf;

# Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
 return 302 https://www.example.com$request_uri;
}


server {
 listen 443 ssl http2 default_server;
 server_name www.example.com;
 include /etc/nginx/conf-available/ssl.conf;

 

 

Redirecting all http and https www.example.com  requests to https://example.com is also very easy. To do that you should use a config similar to this.

 

Redirect www to non-www

server {
 listen 80;
 server_name www.example.com example.com;

# Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
 return 302 https://example.com$request_uri;
}

server {
 listen 443;
 server_name www.example.com;
 include /etc/nginx/conf-available/ssl.conf;

# Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
 return 302 https://example.com$request_uri;
}


server {
 listen 443 ssl http2 default_server;
 server_name example.com;
 include /etc/nginx/conf-available/ssl.conf;

 

 

Change your Website Address in WordPress.

Your WordPress General Settings can be the another reason these redirects are not working for your configuration. If you are using WordPress then don’t forget to change your website address in General settings to match the desired www or non-www domain name.

To change the website address in General Settings,

First go to Settings >> General >> General Settings.

 

how to redirect non-www url to www for wordpress in Nginx
How to redirect non-www url to www for wordpress in Nginx and Apache?

 

Now with all the above configuration and settings your http or https non-www requests should automatically redirect to https://www.example.com

 

Redirect www to non-www in Apache Web Server.

If you want o redirect www.example.com to http://example.com in Apache web server then you should include these settings in your httpd.conf file.

<VirtualHost *>
  ServerName www.example.com
  Redirect 301 / http://example.com/
</VirtualHost>

 

 

If you prefer using .htaccess file in Apache web server for redirecting www to non-www and vice-versa then use these configurations.

 

Redirect non-www to www in Apache Web Server.

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

 

Redirect www to non-www in Apache Web Server.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

 

Hope this post will help you solve any and all the problems that you are having with redirecting urls form non-www to www and vice-versa.

If you like this post then share it with others and if I have missed anything please update us through comment box. I will keep updating the same based on feedback’s received.????

share on:
engy

engy

Hello there, My name is Rishi Guleria and I work as a Linux system administrator. I have created this blog to share what I have learned so far and to learn new things. Good Day. :)

Leave a Response

share on: