0

My website url looks like www.[site-name].com

I want to redirect to https://www.[site-name].com on .htaccess with following code.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301, L]

I copy the file to ftp at this path: httpdocs/.htaccess

img

But it does not redirect to [site-name].com when I try to load www.[site-name].com. How can I solve this?

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58

2 Answers2

1

Try this:

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,QSA]
Luca Jung
  • 1,440
  • 11
  • 25
0

If you want to redirect non-www url to www url , for both http and https use this ;

RewriteEngine on

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

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

If you want to redirect www to non-www url use this ;

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

or without using domainname ;

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]
RewriteCond %{HTTPS_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]
Erdinç Çorbacı
  • 1,187
  • 14
  • 17