Non WWW to WWW redirect with HTACCESS

Non WWW to WWW redirect with HTACCESS

Advert

It really doesn't matter if you use www.yoursite.com or yoursite.com. I prefer non WWW version thats why Tech Stream uses a no www one, you can choose any depending on your views. Having 2 versions same time can cause problems. You can overcome this by forcing a version with 301 redirect from other version.

If you are starting up you can choose any may be toss a coin to find which version to use. If your website is been online for a while, I recommend you to do some analysis In different search engine and find out which version is widely used and make it permanent.

Redirect non-www urls to www with HTACCESS

The following code makes a 301 redirect to WWW version of your website, it redirects users and also bolts informing them bolts that content in those pages are moved Permanently.


RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L] 
                    

You can also use RewriteCond %{HTTP_HOST} ^example\.com [NC] instead of RewriteCond %{HTTP_HOST} !^www\.yoursite\.com. Both serve the same.

non-www urls to www with https using HTACCESS

To force non-www urls www with https


RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]                    
                    
Redirect www urls to non-www for APACHE with HTACCESS

The following code make a permanent 301 redirect to non-www version.


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

You can also use RewriteCond %{HTTP_HOST} ^example\.com [NC] instead of RewriteCond %{HTTP_HOST} !^www\.yoursite\.com. Both serve the same.

Redirect www urls to non-www with https using HTACCESS

If you need to force https along with non-WWW you can use the code below


RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]                
                    
WWW to NON WWW Redirect for Apache with httpd.con

IF you don't have access to .HTACCESS you can use httpd.con, most free web host don't provide access to HTACCESS



  ServerName www.example.com
  Redirect 301 / http://example.com/
                    
                    
NON WWW to WWW Redirect for Apache with httpd.con


  ServerName example.com
  Redirect 301 / http://www.example.com/
                    
                    

Many popular scripts, particular content management systems (CMS's) edit the .htaccess file and add their own redirection so you may not have to add any of the code noted above. Adding these again can lead to 500 errors or improper redirection

Microsoft IIS Web Server

You need to add the code given below to a file that is included on all the pages, or directly on top of each file


If InStr(LCase(Request.ServerVariables("SERVER_NAME")),"www") = 0 Then
    Response.Status="301 Moved Permanently"
    Response.AddHeader "Location", "http://www.domain.com"
End If                  
                  

Note: these instructions require administrative access to IIS. If you do not have this access (e.g., if you have a shared hosting account on a Windows server), you should use one of the server-side scripting methods such as ASP or PHP given further below.

Comments

Wow ! you have someting to tell us. That's great! Please keep in mind that comments are moderated, we employ rel="nofollow" for links, avoid using a spammy word or a domain in name field, it might end up as a Spam. Thanks for reading.

Last 5 Articles

All Articles >

  1. Text Area Widget in Django Admin

     
  2. Control ViewSet Method in Django

     
  3. CSS Colors

     
  4. Creating a Website that Converts

     
  5. Generate Variables from string in Javascript

     

News Letter

Subscribe to our email newsletter for useful tips and valuable resources, sent out every new article release.