Use .htaccess to redirect non www to www

htaccess redirectIt’s is considered good practice, for both SEO and users, to have your website resolve to one URL. Technically speaking www.domain.com is a subdomain of domain.com. Users often type www. prior to the domain in the address bar. So to keep that formality consistent with your users you can redirect the traffic on domain.com to www.domain.com.

.htaccess

If your website is hosted on an apache server, this redirect from non www to www is a simple implementation. Add this to your .htaccess file.

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

Add the above code to you .htaccess file and replace domain.com with your domain name.

I have another post on how to redirect www to non-www with .htaccess.

How to comment in a .htaccess file

comment htaccess fileThe .htaccess file is commonly used to redirect traffic and search engines to pages that have been moved or when an entire site moves to a new domain. Developers will commonly include comments in code to instruct what the next snippet of code will do. This commenting standard helps for projects that are managed by a team or may be transitioned to another developer.

Commenting in the .htaccess file is done by adding a # at the beginning of the line(s) that you would like to comment. The # is required before each line of comment. Essential this instructs the server to ignore that line in the .htaccess file.

Example:

# redirect section
RewriteEngine onRewriteCond %{HTTP_HOST} ^domain-one\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain-one\.com$
RewriteRule (.*) http://www.domain-two.com/$1 [L,R=301] 
# end redirect section 
# another comment line

Using jQuery: Get Current Year

Using jQuery to output the current year is a useful substitute when your website pages are done in HTML and not with server side languages like PHP or ASP. The following code demonstrates how to print the current year in HTML using jQuery.

First, you will need to reference a jQuery library in the <head> of your HTML. You can do this one of two ways:

  • Link directly to the jQuery library on the jQuery code site
<script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
  • Download a version of jQuery from the jQuery site (http://jquery.com/) and relatively link to it from within your site
<script src="/script/jquery-1.4.2.min.js" type="text/javascript"></script>

With the jQuery library referenced you can now pull the current year and push it to an HTML element using some jQuery syntax. The below JavaScript grabs the current year and pushes the value to the “text” of the HTML element with the ID of “year”.

<script type="text/javascript">
  var currentYear = (new Date).getFullYear();
  $(document).ready(function() {
  $("#year").text( (new Date).getFullYear() );
  });
</script>

Now we create the HTML element to receive the year value:

<p>Copyright <span id="year">TEXT</span> Company Name - All Rights Reserved.</p>

With this code in place, the year will be pushed to the TEXT position of the “year” span tag when the DOM is ready. This functionality is useful for elements such as copyright dates or other date representations that need to stay current.