user220755
user220755

Reputation: 4446

Redirecting using 301 rule in .htaccess

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?

Upvotes: 0

Views: 493

Answers (3)

chronos
chronos

Reputation: 443

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

Upvotes: 0

Sonny
Sonny

Reputation: 8336

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.

Upvotes: 0

philfreo
philfreo

Reputation: 43884

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]

Upvotes: 2

Related Questions