-1

I've found SO many questions asking how to do this but NONE of them work for me:

####TRYING TO HANDLE WWW HERE
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,NC,L]
</IfModule>

####WORDPRESS RULES
# 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

####CAKEPHP RULES
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} !^/$
RewriteRule (.*) app/webroot/$1 [L]

I want to make it so that any non-WWW URL is redirected to the WWW.* equivalent. Currently I get a redirect loop no matter what I do.

I'm trying to avoid hardcoding the URL because it needs to work on a couple different servers, including some on subdomains: sub.example.com --> www.sub.example.com

emersonthis
  • 32,822
  • 59
  • 210
  • 375

2 Answers2

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

has always worked for me. You may not need the IfModule stuff. Of course, you are on an Apache server, right? IIS and other Windows servers don't recognize .htaccess.

Phil Perry
  • 2,126
  • 14
  • 18
0

You can try this:

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

It works for me always.

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64