Mod_Rewrite to exisiting .htaccess file

I want to add this code to an already existing .htaccess file to make all non-www URLs to go to their www version to protect against duplicate content issues with Google:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}!
^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1
[L,R=301]
<IfModule>

The exisiting .htaccess file has about 50 lines of permanent redirects like so:
Redirect permanent /BeingPresent.html http://www.equinection.org/programs/awakening.html

Also, this is for a .org domain so I would assume this line would change to:
^www\.equinection\.org [NC]

Is that correct?

How do I add the mod rewrite to the .htaccess file safely and correctly? I tried just adding it and it gave me an error message.

Thanks so much in advance!

Put this at the start of your .htaccess


Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{HTTP_HOST} !www\\.equinection\\.org$ [NC]
RewriteRule .? http://www.equinection.org%{REQUEST_URI} [L,R=301]

:slight_smile:

Thanks so much. I think that worked! I added that to the top then a line space then each permanent redirect listed. Is there a way to check if it’s working or if I did it correctly?

If you request anything that is not [noparse]www.equinection.org[/noparse] and you get redirected to [noparse]www.equinection.org[/noparse] it works as intended. There’s nothing more to it :slight_smile:

You might want to look if the Redirects contain any URLs other than [noparse]www.equinection.org[/noparse] that resolve to your website though, as you will then redirect people twice.

i.e., suppose you have


Redirect 301 /bla http://equinection.org/blahbla

you’d better change that to


Redirect 301 /bla http://[B]www.[/B]equinection.org/blahbla

Otherwise people who request /bla, will get redirected to [noparse]http://equinection.org/blahbla[/noparse] and then to [noparse]http://www.equinection.org/blahbla[/noparse] (2 redirects), instead of being redirected to [noparse]http://www.equinection.org/blahbla[/noparse] directly (1 redirect).
2 redirects is not per se, but it you can do it in one that’s certainly better :slight_smile:

Gotcha. Thanks so much ScallioXTX!

There’s no way to check an .htaccess file via URL right? For example, www.domain.com/.htaccess? You can tell I don’t know much about these. Is there a tutorial on them that you know of?

If your host is configured correctly (and most of them are) the .htaccess can’t be accessed via a browser. If you want to know if yours is: try it :slight_smile:
You should get a 403 forbidden (I think, or a 404 not found, dependent on configuration)

As for a tutorial, here’s a nice one: Apache’s mod_rewrite

Got a 403 error yea…never thought I’d say that :-).

Thanks for the link to the tutorial.

Thanks for everything!