Kumari Manisha
Kumari Manisha

Reputation: 682

How to redirect domain with www or without www

I am using the following rewrite rule to redirect domain that has or has not www.

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

It works fine for any sub domain name like www.domainname.com/abc.php or domainname.com/abc.php. But not adding https in case of www.domainname.com or domainname.com.

I tried adding one more rule

RewriteCond %{HTTP_HOST} ^www\.domainname\.com$
RewriteRule ^https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

But, its not working.

Upvotes: 1

Views: 150

Answers (1)

Panama Jack
Panama Jack

Reputation: 24448

Just use this. Replace example.com with your domain.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

Upvotes: 1

Related Questions