views:

110

answers:

3

I am having a problem with redirecting a page from example.com (to) www.example.com

The code I have is:

  RewriteEngine on

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

And it is not working, any help?

A: 

This is what my rewrite looks like, and it works:

## REWRITE RULES
# enable rewrite
RewriteEngine On
RewriteBase /

# enforce a specific domain in the url
RewriteCond %{HTTP_HOST} !^www\.sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.sub.domain.com/$1 [R=301,NC,L]

This rule will redirect anything that is NOT www.sub.domain.com to www.sub.domain.com.

Sonny
The thing is I am redirecting subdomain.blah.com (to) www.subdomain.blah.com so I need the www (and your solution above did not work unfortunately :( )
I have updated my answer. Notice the exclamation point in my condition. The condition I use doesn't have the `sub` portion.
Sonny
+2  A: 

All you need to do is force the www version of your domain? Just do this...

# Force www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
philfreo
This is what I had and it did not work
That will certainly work for example.com to www.example.com, which is what your question specified, unless you don't have mod_rewrite installed or your Apache config files are not allowing your .htaccess to work. If you have specific subdomain needs, perhaps you should repost with your exact situation.
philfreo
I use this all the time (hundreds of websites, literally) and it works fine. Yours is slightly different because its a subdomain but should work just as well, if it's not, I would check with your host provider and make sure they don't have any restrictions
Kerry
A: 

Update: based on your comment It does not redirect and it gives me the openDNS page, I believe your problem is not (yet?) with mod_rewrite.

Try adding Options +FollowSymlinks before RewriteEngine On, then add RewriteBase / after RewriteEngine On.

I recommend reading sections Fatal Redirection and rewrite logging found here.

You can enable mod_rewrite debugging to see what is going on (if anything!):

RewriteLog "/tmp/rewrite.log"`
RewriteLogLevel 5
chronos