You are having problems with mod rewrite non www to www

Can anyone help me with this:

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

What I'm trying to do is create a rewrite rule that will send you to the www version of the site if you try to connect using a non-www version.

The condition works, but there are no rules, it sends me to http: // Can someone suggest a hoe, I can fix it. I expected $ 1 = everything in the condition above between ^ and $ Thanks

+3
source share
1 answer

Do not use HTTP_HOST, this is evil .

Do it:

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

Where %1corresponds to the grouping from the previous one RewriteCond.

+4
source

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


All Articles