How to redirect URL without WWW to URL with WWW using .htaccess

How to redirect URL without WWW to URL with WWW using .htaccess.

Create a .htaccess file with content in frame bellow and upload it to server. This will redirect URLs without www to URLs with www. This rewrite rule also remains http/https and absolute path after hostname.

RewriteEngine on
 
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301]

Applies To

IIS, Web server, Web site, URL Rewriter, .htaccess

See Also