Redirecting non www. to www. in WordPress

rugbyjack2005

Power Member
Joined
Oct 2, 2011
Messages
539
Reaction score
50
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.
 
try this code, insert it before any other code

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
 
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.

Exactly, I never have to modify the .htaccess of my wordpress websites.
 
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.

What this guy says is the easiest way. I only use rewrite on my ecommerce/html sites!
 
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. No need to put the domain name in at all right?
 
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, I will have a look at that
 
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.
 
This didn't work annoyingly. Any other ideas?

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
 
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.

Got here to say this.
 
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

Hmm, I have double checked and the code is exactly how you have it above obviously with the actual domain name.
 
Hmm, I have double checked and the code is exactly how you have it above obviously with the actual domain name.

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
 
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

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
 
Back
Top