Can I just Confirm How to Perform a 301 Redirect for non www URL's

Hi All

I appreciate any help with this. I’ve been reading on the web regarding how to point the non www version of my domain to the www version.

I understand that I need to set up a 301 redirect, (and have sample coding).

However, I cannot see how to target the non www version.

Do I simply add the coding to the .htaccess file? I’m a bit confused by this, so would greatly appreciate the clarification.

Didcot84

You target the non-www version by using either


RewriteCond %{HTTP_HOST} ^domain\\.com$ [NC]

That is, the version of the domain without www, or you could use


RewriteCond %{HTTP_HOST} !^www\\.domain\\.com$ [NC]

which is: anything else than the www-domain

Does that make sense?

Thank you so much Scallio,

So Can I just confirm,

  1. I create a text file that contains the code that you have sent through
  2. I name it .htaccess
  3. I save it into the root of my site? Not the public_html folder, but the root itself?

Really appreciate your help here, I do not know much about apache, just want to know how to do this one task?

1 and 2 are correct, 3 is not.
.htaccess files are saved in the public_html/ directory, not in the root
(altough public_html/ is the root URL of your domain, so in some sense one might say that the public_html/ directory is the root directory. I’m probably just confusing you at this point so feel free to forget what I just said ;))

Thanks so much Scallio, that will set me up nicely for all of my websites.

I notice that you’re from holland, are you a football fan? I support liverpool, so dirk kuyt is a key player.

just to confirm, for a real site, I need the [NC] at the end of the line

lol, no I’m not really a football fan (except with the EC and WC), but I’m glad that you’re happy with our Dirk :slight_smile:

As for the [NC], yes you do need that. It stands for No Case and basically says that Apache shouldn’t match case sensitive, i.e., DOMAIN.COM, Domain.Com, DoMaIn.CoM, etc, should also be redirected, and not not just domain.com (which would happen if you leave out the [NC]).

Brilliant, thanks again mate. Really appreciate you taking the time

Sorry mate, if I’ve created the file and uploaded it, how would I be able to test that it’s working?

I thought that if I keyed in the non www version, the redirect would change the url to the www version? But this isn’t happening?

Here is a link to the site : -

http://bit.ly/lL9G5Q

Could you please post the complete .htaccess file you’re using now?

Thanks Remon

I have a created a one line file with

<code>
RewriteCond %{HTTP_HOST} !^www\.jefferyandwilkes\.co.uk$ [NC] </code>

saved it as .htaccess in the public_html folder

How does that sound?

Sounds like you need thee more lines :wink:


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\\.jefferyandwilkes\\.co\\.uk$ [NC]
RewriteRule .? http://www.jefferyandwilkes.co.uk%{REQUEST_URI} [L,R=301]

I’ve put a \ before “.uk” in the RewriteCond as well.

:slight_smile:

Thanks very much Remon

That has now worked. Can I just confirm that this code would work for all future websites, I would simply change the domain name?

Correct :slight_smile:

Remon, I would like to thank you in some way for your time and effort, please let me know if I can show my appreciation.

I have tried to add the 301 redirect to a different website.

Web Site

However, when I do so, the site goes down and brings up a 500 server internal error message.

I do not know anything about the server, would that message mean that the server is not an apache server?

The coding that I have used is

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.freshwaterwindowcleaners-nw\.co\.uk$ [NC]
RewriteRule .? http://www.freshwaterwindowcleaners-nw.co.uk%{REQUEST_URI} [L,R=301]

Thank you again for your help

Looks okay to me. Does it work if you put a backslash in front of the dash in the RewriteCond (it shouldn’t be needed, but a dash is a reserved character in regular expressions).

Maybe the server doesn’t support mod_rewrite at all? Take a look at the test at Apache’s mod_rewrite (under the heading “Test”).

As for showing appreciation, that’s not necessary. I’m just here because I like to help, not to make money. Tanks anyway :slight_smile:

Thanks Remon, I’ll test that out tomorrow. If it doesn’t work, would I have another option?

Well all I can say to thank you is that your replies have truly made a difference and I thank you greatly.

I’m afraid not, no. If mod_rewrite isn’t enabled you could ask your host to enable it, but that’s about it.

:slight_smile: