Shopping Cart Forum

Go Back   eShop Forums - eCommerce Help Forum for Shopping Cart Owners. > Tech Corner > PHP/HTML/CSS/JavaScript and Servers
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
PHP/HTML/CSS/JavaScript and Servers Pick up tips and exchange info on the technical side of having a web presence.

Reply
 
LinkBack (6) Thread Tools Display Modes
  6 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 26-10-2007, 02:21 PM
Lawrence Lawrence is offline
Forum Admin
 
Join Date: Aug 2007
Location: Kenley, Surrey, UK
Posts: 1,226
Thanks: 2
Thanked 82 Times in 82 Posts
Default How to 301 redirect non-www URLs to www ones and vice versa using .htaccess

Sometimes you may find that you have both www and non-www versions of your site e.g http://www.mysite.com & http://mysite.com indexed in the SERPs which not only looks messy but could possibly lead to your site being penalised for duplicate content. One method to prevent this is to use Apache's mod_rewrite to change the URLs.

This is how you would use it to change non-www to www

Code:
RewriteEngine On
 
RewriteCond %{HTTP_HOST} ^MySite.com [NC]
RewriteRule ^(.*)$ http://www.MySite.com/$1 [L,R=301]
and this is how you would do it in reverse i.e. change www to non-www URLs

Code:
RewriteEngine On
 
RewriteCond %{HTTP_HOST} ^www.MySite.com [NC]
RewriteRule ^(.*)$ http://MySite.com/$1 [L,R=301]
There's much more that can be done with Apache's mod_rewrite rules. To use them to their full potential, I'd recommend acquainting yourself with regular expressions for which you'll find many tutorials on the net.
__________________
Lawrence
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
.htaccess, 301 redirect, url rewrite

LinkBacks (?)
LinkBack to this Thread: http://www.eshopforums.com/f31/how-301-redirect-non-www-urls-www-ones-vice-versa-using-htaccess-632/
Posted By For Type Date
301 re-direct - Page 2 - UK Business Forums - the UK's most active help and advice forum for owners, managers and entrepreneurs of small businesses and startups. This thread Refback 10-05-2008 12:51 PM
301 re-direct - Page 2 - UK Business Forums - business help and advice for owners, managers and entrepreneurs of small businesses and startups This thread Refback 20-02-2008 08:30 PM
Digg / Linux/Unix / Upcoming This thread Refback 27-10-2007 12:27 AM
Digg / Linux/Unix / Upcoming This thread Refback 26-10-2007 07:47 PM
Digg / Linux/Unix / Upcoming This thread Refback 26-10-2007 02:45 PM
Digg - How to 301 redirect non-www URLs to www ones and vice-a-versa. This thread Refback 26-10-2007 02:43 PM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use .htaccess to block IPs and domains from your website. Lawrence PHP/HTML/CSS/JavaScript and Servers 1 04-07-2008 10:40 PM
How to use mod_rewrite to block bad bots with .htaccess Lawrence PHP/HTML/CSS/JavaScript and Servers 0 30-10-2007 04:25 AM
How to perform a HTTP 301 redirect in PHP (Search engine friendly) Lawrence PHP/HTML/CSS/JavaScript and Servers 0 29-09-2007 12:37 AM
How to do a page redirect using HTML meta tags. Lawrence PHP/HTML/CSS/JavaScript and Servers 2 26-08-2007 11:20 AM

Google

Edible Graphics, Affordable E-Commerce, Web Shops & Custom Form Scripts
Fair Trade Quality Handmade Photo Albums & Gifts
tech news, product reviews, the latest home and business technology, the latest in digital imaging


Content Relevant URLs by vBSEO 3.0.0