Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Web Servers ›
Redirect non www to www

Redirect non www to www

November 20, 2010 Web Servers Jesin A Leave a Comment

http status code redirect 301

This article explains how to redirect your website from the non www url to the www url but editing the .htaccess file. This is necessary because search engines think that example.com and www.example.com are different websites. But since they both have the same content it might work negatively on the search engine ranking. So it is better to redirect example.com to www.example.com doing so this sends HTTP 301 status code when a search engine (or any user) accesses example.com. The 301 code says the non www domain has been “Moved Permanently” to the www domain. Before proceeding make sure the server has support for mod_rewrite.

Redirect non www to www htaccess

If it doesn’t already exist create a file named .htaccess (notice the dot at the beginning) inside the Document Root i.e., the www or public_html or htdocs folders. If you already have the .htaccess file edit it using a text editor. Add the following line to the .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Replace example.com with your domain name. Save the .htaccess file. Test the configuration by typing example.com in your browser and check whether it redirects to www.example.com. Now to technically test it. Go to HTTP Headers Lookup Tool enter your domain name WITHOUT www and you should see something like this

http status code redirect 301
The status code returned is 301 which means redirect from non www to www using htaccess was successful

Take a look at the first two lines. The first displays the status code and second shows where it redirects to. Note to Joomla users – if you are using Joomla’s SEO friendly URLs feature using .htaccess file there is no need to enter the first line (RewriteEngine On) again as the .htaccess file shipped with Joomla already contains this.

Related posts:

Default ThumbnailUsing the Apache Redirect directive Default ThumbnailRedirect http to https using htaccess Default ThumbnailHow to create custom error page in Apache Default ThumbnailRunning PHP code in HTML files Default ThumbnailSetting a Timezone in PHP

Tags: htaccess, redirect

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.