Score:0

Apache RewriteRule is not redirecting to www

mx flag

Using apache 2.4 and my site is on www.example.com, which automatically gets redirected to https://www.example.com, wherein it is fully accessible and functional.

However, when I explicitly try using http://example.com or https://example.com, I get greeted by a placeholder page.

What I would like to happen is to redirect non-www to www. Ex (https://example.com -> https://www.example.com

I have set up a rewrite rule in my .conf file, but for some reason, it's not working. All I'm seeing is a placeholder page when accessing non www.

<VirtualHost <ip>:80>
    ServerName www.example.com
    ServerAlias example.com
    # Redirect to www
    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost <ip>:443>
    ServerName www.example.com
    ServerAlias example.com
    SSLCertificateFile /home/admin/certs/www_example_com.crt
    SSLCertificateKeyFile /home/admin/certs/www_example_com.key
    SSLCACertificateFile /home/admin/certs/DigiCertCA.crt
    SSLEngine on
    SSLProtocol +TLSv1.2 -TLSv1.1 -TLSv1 -SSLv3 -SSLv2
    DirectoryIndex index.php index.html index.htm
    # redirect to www
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Score:0
us flag

You forgot to activate RewriteEngine.

    RewriteEngine on
demiglace avatar
mx flag
Added the RewriteEngine on to the vhost for https traffic, I still get the placeholder page, and not getting redirected to www, when accessing http://example.com or https://example.com
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.