2

I use this

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://kanzan.se%{REQUEST_URI} [L,NE,R=301]

to redirect ALL www to non-www, but it doesn't work when I type https://www.kanzan.se. The www is still there!

user2908112
  • 167
  • 1
  • 13

2 Answers2

1

To redirect https://www to non www use the following:

RewriteCond %{HTTP_HOST} ^www.your_domain.com$
RewriteRule ^(.*)$ http://your_domain.com/$1 [R=301]

To redirect www to non-www (while using SSL)

RewriteCond %{HTTP_HOST} ^www.your_domain.com$
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^(.*)$ https://your_domain.com/$1 [R=301]

after this don't forget to enable the rewrite mode.

sudo a2enmod rewrite;
sudo service apache2 restart;
Sukhjinder Singh
  • 1,994
  • 2
  • 9
  • 17
0

Ok I got it working. I used this

RewriteCond %{HTTP_HOST} ^www.kanzan.
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^ https://kanzan.se%{REQUEST_URI} [L,R=301]

in my virtual host for 443. Before I did put all in the global config. I don't understand why it made any difference..

user2908112
  • 167
  • 1
  • 13