Search code examples
.htaccesshttp-redirectmod-rewriteurl-rewritinghttp-status-code-301

Redirect non www to www sub pages also


in .htaccess Am using :

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

It works fine for main domain i.e. redirects example.com to https://www.example.com

but not works on sub pages like

site.com/blog not redirects to https://www.example.com/blog

also like to add tailing slash '/' after url

https://www.example.com/blog/

how to achieve this?

Thanks in advance!


Solution

  • You should have htaccess rule like this. Make sure you keep these rules at the top of your htaccess file. Please clear your browser cache before testing your URLs.

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