close ad
 
Important WebAssist Announcement
open ad
View Menu

Web development tutorial

Add a www redirect: Avoid Google indexing your website twice

Tutorial created by Christopher West, Freelancer Looking for Work

Categories: SEO

rating

One important factor when dealing with SEO is how Google interprets your website in its indexes.

When you register a website you will have something like:
http://www.domain.com (.co.uk etc)

However when Google indexes your website it will see:
http://www.domain.com (.co.uk etc)
AND
http://domain.com (.co.uk etc)

Google will class this as 2 separate websites to index, thus will affect SEO ranking since it assumes its duplicate content....

arrow downWhats needed

A simple text editor such as Note Pad

arrow downThe .HTACCESS Approach

The first approach uses a special file called the .htaccess (notice, there is a 'period' in front of htaccess suggesting that this is the file extension and thus no filename).

This file corrosponds to LINUX based systems only.

This approach is for anyone who has access to uploading to a hosting server for their website and the ability to upload a .HTACCESS file.

Step 1: The code

Copy this code into your chosen text editor and change the "yourwebsite" reference to what ever website URL you are referencing.

# Redirect non-www urls to www
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yourwebsite\.co.uk
RewriteRule (.*) http://www.yourwebsite.co.uk/$1 [R=301,L]

Step 2: Saving the file (a special way)

Ok so you have the code in place in your chosen text editor, you canot simple save it as you usually would as this would then prefix the extension .txt to the file your saving (so it ends up being .htaccess.txt) ...That would then make the file unoperational for the Linux server.

What you need to do is make sure SAVE AS is selected and then select ALL FILES then simple type in .htaccess (remember the period in front of the htaccess).

Step 3: Uploading the file (a special way)

ok so you sucessfully saved the file your now ready to upload it to the websites hosting server...whats important here is that in your chosen FTP software that you select ASCII as the uploading type for this file....very important!

You should be uploading this file to the root directory of your website...the way it works is that you could potentially have a different .htaccess file in different directories on your website hosting server which can perform so many different things...but for "sitewide" operation you would need to make sure its uploaded to the root for your website.

I use SmartFTP for uploading to my hosting servers and here is a screenshot of where to select ASCII for the uploading of the .HTACCESS file.

arrow downThe PHP Approach

This was kindly sent to me by WebAssist one and only Ray Borduin. And its a different approach in case you dont have direct access to uploading files or have access to uploading a .htaccess file (perhaps your hosting provider prevents that type of file from being uploaded).

Step 1: Use this to add www to the URL

Add this code to the top of everypage...or copy it to a new page save it using the .php file extension and then use a php include to include it to what ever pages you want...or if required for site wide integration then add this code to your dreamweaver template or WebAssist Framwork Builder template.

php:
//make sure www is in the url
if(strpos($_SERVER['SERVER_NAME'], 'www.') === false) {
 $redirect = ((!empty($_SERVER['HTTPS'])) ? 'https://' : 'http://') . 'www.' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
 header('Location: ' . $redirect);
}

Step 2: Use this to remove the www

Add this code to the top of everypage...or copy it to a new page save it using the .php file extension and then use a php include to include it to what ever pages you want...or if required for site wide integration then add this code to your dreamweaver template or WebAssist Framwork Builder template.

php:
//remove www from the url and redirect only if www is in the url
if(strpos($_SERVER['SERVER_NAME'], 'www.') === 0) {
 $redirect = (!empty($_SERVER['HTTPS']) ? 'https://' : 'http://') . substr($_SERVER['SERVER_NAME'],4) . $_SERVER['REQUEST_URI'];
 header('Location: ' . $redirect);
 die();
}

arrow downFor Fun ...

Name other uses for the .HTACCESS file ...

What else can it allow the developer to do on a website??

arrow downReviews and comments

Comments will be sent to the author of this tutorial and may not be answered immediately. For general help from WebAssist, please visit technical support.

Sign in to add comments
rating
rating

Jamie: 11 Years, 5 Months, 1 Week, 6 Days, 13 Hours, 14 Minutes ago

I also use the .htaccess file to restrict access to accidental visits to the uploaded files folder... you just create another .htaccess file and in it all you have to do is put Options -Indexes and then upload that to the folder you wish to restrict accidental access to.

It still allows the site to reference images/files that have been uploaded as well as allowing the site owner to access the file manager.

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.