📜 ⬆️ ⬇️

Getting rid of www in the site address

A universal solution to the problem of constant redirects to a domain without www using mod_rewrite. Simple and useful. Works on any domain without having to edit for each site. Earlier on Habré saw similar, but without independence from the domain name.
At the root, we drop the .htaccess file with the following contents:
  <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 # Put the protocol into the% {ENV: PROTOCOL} environment variable
 RewriteRule ^ (. *) $ - [E = PROTOCOL: http]
 RewriteCond% {https} ^ on $ [NC]
 RewriteRule ^ (. *) $ - [E = PROTOCOL: https]
 # If you need to remove www
 RewriteCond% {SERVER_PORT} ^ 80 $
 RewriteCond% {HTTP_HOST} ^ www \. (. *) [NC]
 RewriteRule ^ (. *) $% {ENV: PROTOCOL}: //% 1% {REQUEST_URI} [R = 301, L]
 RewriteCond% {HTTP_HOST} ^ www \. (. *) [NC]
 RewriteRule ^ (. *) $% {ENV: PROTOCOL}: //% 1:% {SERVER_PORT}% {REQUEST_URI} [R = 301, L]
 </ IfModule> 

')

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


All Articles