简体   繁体   中英

Force “www.” via .htaccess

I want to force "www." on my URLs (eg http://domain.com becomes http://www.domain.com ). However, I don't want it forced on URLs that already have a subdomain (eg http://images.domain.com should NOT become http://www.images.domain.com ). The following snippet I found on the net does the latter:

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

What do I need to do to get this to work for me? Thanks.

Simple The rules below force www, except for subdomains.

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

Edit and paste it, restart apache.

Explain:

RewriteCond %{HTTP_HOST} =name.domain [NC] match only when someone types name.domain (your domain name).

When types subdomain.name.domain the RewriteCond is false and not redirect. You understand? In rule that you previously posted you was matching for !(not)^(beginning by)www and subdomain.name.domain satisfied RewriteCond and is what you don't want. :)

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