Redirect URLs without www to www

I need your help. I want to check if a URL is entered without www

how example.comit should be redirected to www.example.com.

+5
source share
3 answers

Try this mod_rewrite rule:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
+12
source
RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^.*$ http://www.example.com/$0 [NC,L,R=301]
+2
source

If you are using nginx, add this line to nginx config:

server {
  listen 80;
  server_name yourdomain.com;
  rewrite ^/(.*) http://www.yourdomain.com/$1 permanent;
}
+2
source

All Articles