-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

5
  • Those rules don't loop for me, you got some other rules somewhere that's redirecting?
    – Jon Lin
    Jan 10, 2014 at 18:08
  • Do you have more rules in your .htaccess?
    – anubhava
    Jan 10, 2014 at 18:21
  • @anubhava These rules are at the top of my .htaccess file. Below is the standard Wordperss rules. Jan 10, 2014 at 18:53
  • Ok in that case most likely it is due to WP itself. Make sure home and site URL are also www in WP permalink settings.
    – anubhava
    Jan 10, 2014 at 18:59
  • @JonLin You're right... I think that lower down rules are interfering. I've updated the question. Jan 10, 2014 at 19:02

2 Answers 2

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.

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.

2
  • I'm trying to avoid hardcoding the URL because it needs to work on a couple different servers (for testing) Jan 10, 2014 at 18:02
  • You should add this part to your question please. Jan 10, 2014 at 18:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.