Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: .htaccess / redirect to 'www.'

  1. #1
    Snak3's Avatar
    Snak3 is offline Moderator
    Join Date
    Jul 2009
    Location
    Undisclosed Location
    Posts
    629
    Thanks
    155
    Thanked 190 Times in 121 Posts

    .htaccess / redirect to 'www.'

    I have a website that i'm currently building and both its main addresses, i.e. with 'www.' and without open up seperately, i.e. there is no redirection.
    I wish to keep the 'www.' one and redirect the 'non-www.' to the 'www.', what code/commands do i have to add to the .htaccess file?
    Thanks

  2. #2
    Aquarezz's Avatar
    Aquarezz is offline Master Net Builder
    Join Date
    Dec 2008
    Location
    Belgium
    Posts
    3,852
    Blog Entries
    4
    Thanks
    683
    Thanked 591 Times in 463 Posts
    Do you have cPanel? You can go to 'redirect' and redirect your domain without www to your domain with WWW. At least that's how I do it

    Greetz
    |Nico Lawsons

  3. #3
    Aziz's Avatar
    Aziz is offline no investment, no glory
    Join Date
    May 2009
    Location
    IL
    Posts
    736
    Thanks
    588
    Thanked 243 Times in 168 Posts
    I use this one

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    does the job nicely

  4. #4
    Snak3's Avatar
    Snak3 is offline Moderator
    Join Date
    Jul 2009
    Location
    Undisclosed Location
    Posts
    629
    Thanks
    155
    Thanked 190 Times in 121 Posts
    Actually i want to do it with htaccess. My site is on Joomla, after renaming the htaccess.txt to .htaccess i added the following code to the end :

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    Am unsure if its right. Doesn't seem to work, checked multiple sites and all have different variations of code which ultimately does the same thing.

    Can some1 give me the code what they are using for this redirection on their site?
    Thanks.

    Edit : @ Aziz, didn't see your post before typing out this. Anyways thanks

  5. #5
    Snak3's Avatar
    Snak3 is offline Moderator
    Join Date
    Jul 2009
    Location
    Undisclosed Location
    Posts
    629
    Thanks
    155
    Thanked 190 Times in 121 Posts
    Was reasearching a bit using Google and found the following variations of code :

    Code:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
    RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
    </IfModule>
    Code:
    Options +FollowSymLinks 
    RewriteEngine on 
    RewriteCond %{HTTP_HOST} ^yoursite.com [NC] 
    RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]
    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^yuordomain\.com
    RewriteRule ^(.*)$ http://www.yuordomain.com/$1 [R=permanent,L]

    Currently using the second one, lets c if it works.

  6. #6
    Snak3's Avatar
    Snak3 is offline Moderator
    Join Date
    Jul 2009
    Location
    Undisclosed Location
    Posts
    629
    Thanks
    155
    Thanked 190 Times in 121 Posts
    2nd one didn't work, Currently using the third one and it works just fine

  7. #7
    Canonical is offline Unknown Net Builder
    Join Date
    Jul 2009
    Posts
    35
    Thanks
    0
    Thanked 19 Times in 11 Posts
    I use the following in the .htaccess in my web's root folder:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^yourdomain\.com$
    RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
    I avoid the !www type rewrite conditions because they can sometimes lead to issues w/ subdomains depending on how they are implemented.

  8. #8
    Dr. Stavros is offline Net Builder
    Join Date
    Jan 2009
    Posts
    152
    Thanks
    36
    Thanked 32 Times in 22 Posts
    FYI, this is the one I finally settled on (after a bit of searching around):

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com$ [NC]
    RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R=301,L]
    Works pretty good for me (but yeah, perhaps subdomains would break)!

  9. #9
    sachin410 is offline Newbie Net Builder
    Join Date
    Jul 2009
    Posts
    77
    Thanks
    11
    Thanked 1 Time in 1 Post
    I added this today and it seems to work fine:

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$
    RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
    what does this do?

    Code:
    Options +FollowSymLinks
    note: when you replace this, dont use masked URL. thanks ~ admin

  10. #10
    vectro's Avatar
    vectro is offline I Like Stuff
    Join Date
    Dec 2008
    Location
    U.S.A.
    Posts
    632
    Thanks
    192
    Thanked 104 Times in 82 Posts
    Quote Originally Posted by sachin410 View Post
    what does this do?

    Code:
    Options +FollowSymLinks
    If a directory is symbolically linked in Linux, then +FollowSymLinks tells Apache to obey them. Sym links are basically shortcuts. For example, if /home/username/www is symbolically linked to /home/username/public_html (both are the same directory) then Apache will allow either path to work for various requests.

  11. Thanked by:

    bogart (30 May, 2010)

Page 1 of 2 12 LastLast

Similar Threads

  1. .htaccess All Page Redirect
    By ProxySites.ca in forum Managing
    Replies: 7
    Last Post: 28 May, 2010, 22:52 PM
  2. .htaccess
    By dodolls in forum Managing
    Replies: 6
    Last Post: 15 November, 2009, 06:58 AM
  3. .htaccess help needed - dynanmic redirect
    By DomainMagnate in forum Programming
    Replies: 13
    Last Post: 27 September, 2009, 11:52 AM
  4. .htaccess help
    By Sami4u in forum Building
    Replies: 3
    Last Post: 14 September, 2009, 13:47 PM
  5. .htaccess - How to properly redirect a domain?
    By Shenron in forum Programming
    Replies: 10
    Last Post: 9 January, 2009, 08:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •