简体   繁体   中英

htaccess how to redirect both www.domain.com AND domain.com to www.realdomain.com

I have an htaccess redirect set up for a bunch of domains to all point towards my main domain - however they only redirect domain.com to www.realdomain.com , if you hit www.domain.com the redirect doesn't take place.

Here's what I have (rewritecond and rewriterules are replicated for each domain):

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.realdomain.com/$1 [R=301,L] 

My knowledge of regexp is a bit limited but I gather that it looks for an address BEGINNING with domain.com , so it makes sense it wouldn't match www.domain.com .

Is there a way I can make that regexp match both of them without having to double the number of rules I already have?

You can have it optionally also match www. by changing the line to rewritecond %{http_host} ^(www\\.)?domain.com [nc]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM