The power of "www." via .htaccess

I want to force "www". on my urls (e.g. http://domain.com becomes http://www.domain.com ). However, I do not want it to be tied to URLs that already have a subdomain (for example, http://images.domain.com should NOT become http://www.images.domain.com ). The following snippet I found on the net has the latter:

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

What do I need to do to make me work for me? Thanks.

+4
source share
1 answer

The simple rules below make www use other than subdomains.

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

Edit and paste it, restart apache.

I explain:

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

When subdomain.name.domain RewriteCond types are false and are not redirected. You understand? In the rule that you previously posted, you were matched for! (Not) ^ (starting at) www and subdomain.name.domain are satisfied with RewriteCond and this is what you don't want. :)

+5
source

Source: https://habr.com/ru/post/1338555/


All Articles