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
Rewrit开发者_开发百科eCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.subdomain.domain.com/$1 [L,R=301]
And it is not working, any help?
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]
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
.
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
精彩评论