1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Redirecting non www. to www. in WordPress

Discussion in 'Black Hat SEO' started by rugbyjack2005, Jun 4, 2013.

  1. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    Can someone help me this please? I am looking to add a redirect to one of my sites that's built in WordPress so the non www. version of the site redirects to to www. version of the site.


    The site already has the standard WordPress .htaccess file with the following code in it:


    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>


    # END WordPress


    I have tried adding a separate .htaccess file with the following code but it didn't work:


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


    I have also tried combining the 2 files with the following code but that didn't work either:


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


    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>


    # END WordPress


    What am I doing wrong? Thanks in advance for any help.
     
  2. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,290
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
    try this code, insert it before any other code

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
     
    • Thanks Thanks x 1
  3. R4Z0rA07

    R4Z0rA07 Junior Member

    Joined:
    Apr 17, 2012
    Messages:
    167
    Likes Received:
    150
    Simplest way is to add www into the domain name in the wordpress settings > site url and the other box with the url which I can't remember now.
     
    • Thanks Thanks x 4
  4. Izzma

    Izzma Regular Member

    Joined:
    Jan 29, 2012
    Messages:
    293
    Likes Received:
    197
    Location:
    Canada
    Exactly, I never have to modify the .htaccess of my wordpress websites.
     
  5. Revolate

    Revolate Regular Member

    Joined:
    Aug 2, 2011
    Messages:
    346
    Likes Received:
    171
    Occupation:
    Hybrid, I do everything!
    Location:
    Earth
    What this guy says is the easiest way. I only use rewrite on my ecommerce/html sites!
     
  6. PeaceLoveSubmission1

    PeaceLoveSubmission1 Regular Member

    Joined:
    Sep 30, 2011
    Messages:
    482
    Likes Received:
    256
    Yes look in the settings its already built in for you
     
  7. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    Thanks. No need to put the domain name in at all right?
     
  8. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    Thanks, I will have a look at that
     
  9. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,290
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
  10. Kixstart

    Kixstart Junior Member

    Joined:
    Apr 15, 2013
    Messages:
    161
    Likes Received:
    62
    Location:
    Old Europe
    Use this on your .htaccess:
    Code:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^yoursite\.com
    RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=permanent,L]
    
    
    
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    
    # END WordPress
    Only changing that thing in the wordpress settings page won't help. The code above redirects all your website contents to www. Also, don't forget to set the preferrential website with www.
     
  11. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    This didn't work annoyingly. Any other ideas?
     
  12. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,290
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
    the code i pasted you i use across all my sites and works flawlessly. If it didn't work for you then you might did something wrong or there's some configuration issue at your apache
     
  13. Kimi

    Kimi BANNED BANNED

    Joined:
    Jan 21, 2008
    Messages:
    860
    Likes Received:
    2,472
    Got here to say this.
     
  14. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    Hmm, I have double checked and the code is exactly how you have it above obviously with the actual domain name.
     
  15. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,290
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
    i said to paste it as is, no modification of the code is necessary. it works for any domain

    here it is again:

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ [URL]http://www.%[/URL]{HTTP_HOST}/$1 [R=301,L]
    
    simply copy & paste, no mods
     
  16. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    Sorry, I was talking about the code kixstart wrote.

    With your one, do you miss out the wordpress code? I have tried it with and that didn't work either