1

I have installed https on my domain. And all this cases are available of my website:

http://example.com        // will be redirected to https://example.com
https://example.com
http://www.example.com    // will be redirected to https://www.example.com
https://www.example.com

See? everything will be using https protocol. All fine.


Now I need to redirect all www to non-www. So http://www.example.com and https://www.example.com should be redirected to https://example.com (in other word, all cases should be redirected to this).

Here is my current code in the /etc/apache2/sites-avalible/000-default.conf:

<VirtualHost *:80>
.
.
.
RewriteEngine on
RewriteCond %{SERVER_NAME} =lamtakam.com [OR]
RewriteCond %{SERVER_NAME} =www.lamtakam.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]               -- this
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]              -- and this are for www redirection
                                                        -- but doesn't work
</VirtualHost>

any idea?

also lamtakam is my exact domain I was talking about, if you need to check something.


Edit:

Currently I have this inside .htaccess file on the root of my project:

RewriteEngine on

Options -Indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# this doesn't work on some version of xampp
# RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]

RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]

ErrorDocument 404 /error404.html
Martin AJ
  • 6,261
  • 8
  • 53
  • 111

1 Answers1

3

If you want rules in VirtualHost then it would be 2 set of rules. If you can keep rules in site root .htaccess then it would be a single rule. I am providing .htaccess rule here:

RewriteEngine On

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

Make sure you remove your existing rules shown in VirtualHost above and you test in a new browser to avoid old browser cache.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Ah thank you .. just one thing, should I remove all `Rewrite` lines inside `Virtualhost`? does your code supports both http to https redirection and www to non-www redirection? – Martin AJ Oct 17 '18 at 19:06
  • Since you already have a .htaccess, place this rule just below `RewriteEngine` line and **remove** every line that starts with `Rewrite` in `Virtualhost`. And yes of course this rule takes care of both `http -> https` and `www` removal. – anubhava Oct 17 '18 at 19:20
  • I see, thank you so much .. just please take a look at this, and tell me if it is correct: [the content of my `.htaccess` file](https://gist.github.com/sajadshafizadeh/b8ed348a8f87302cddc4d89639365c92) ? – Martin AJ Oct 17 '18 at 19:26
  • 1
    Yes that looks correct to me. May be you can move `ErrorDocument` and `Options` before `RewriteEngine` – anubhava Oct 17 '18 at 19:32
  • 1
    I've tested your rule and worked well exactly the same as my expected result. Thank you so much. Also for my information *(another question)*: when I've uploaded my website, `.htacess` file wasn't known for apache. And I've added [this](https://gist.github.com/sajadshafizadeh/4a4da3bf2aecb645a474c004125f560f) inside `/etc/apache2/sites-availible/000-default.conf` file and then `.htaccess` worked. now I want to know, can I place [this](https://gist.github.com/sajadshafizadeh/4a4da3bf2aecb645a474c004125f560f) inside `.htaccess` too? – Martin AJ Oct 17 '18 at 20:07
  • Unfortunately `Directory` is not allowed inside .htaccess – anubhava Oct 17 '18 at 20:21