301 redirect non-ww to www

I am heaving issues with redirecting non-www URLs to www ones.

My htaccess contains this code:

RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} !/ow_updates/index\.php
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} !/ow_cron/run\.php
RewriteCond %{REQUEST_URI} !/e500\.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.xml|\.feed|robots\.txt|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php

How to combine the code above with this one:

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

Put the “to www” before the other block of code. That’s all.

lf,

What J_M said AND

Make sure that you use the No Case flag any time you’re examing (trying to match) the {HTTP_HOST} variable (you should NOT be usingt it when matching the {REQUEST_URI} variable as you did in your last RewriteCond statement). In addition, no need to capture the :fire: EVERYTHING :fire: atom as it’s already in the {REQUEST_URI} variable. Therefore:

[code]# {Your Apache core code, if any}

{Your mod_rewrite code}

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

{Your other code}

[/code]

No need to use the QSA, either as the {QUERY_STRING}'s contents are unaffected (and will be sent along with the redirection).

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.