开发者

Force "www." via .htaccess

开发者 https://www.devze.com 2023-02-08 23:11 出处:网络
I want to force \"www.\" on my URLs (e.g. http://domain.com becomes http://www.domain.com). However, I don\'t want it forced on URLs that already have a subdomain (e.g. http://images.domain.com should

I want to force "www." on my URLs (e.g. http://domain.com becomes http://www.domain.com). However, I don't want it forced on URLs that already have a subdomain (e.g. http://images.domain.com should NOT become http://www.images.domain.com). Th开发者_StackOverflow社区e 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. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号