Rewrite Rules for multiple subdomains and https

Posted January 16, 2017 5.8k views
UbuntuApacheLAMP Stack

I’m having an issue with (no www). This URL is serving the ubuntu default page, but, both redirect to and server the correct website from the correct location. I would like (no www) to also redirect to

I’m having trouble getting all of the redirects to work at the same time. Im not sure what I’m doing wrong as I don’t have a ton of experience with rewrite rules. Any suggestions would be greatly appreciated.

Here’s my conf file.

<VirtualHost *:80>
    DocumentRoot /var/www/thedenveradventure/public_html

    ErrorLog /var/log/php_errors.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/thedenveradventure/public_html>
        RewriteEngine On

        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all

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


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer


For the HTTPS redirect, you should be able to use the below as long as you also have a VirtualHost block configured for access on Port 443 (for SSL), otherwise if you only have a VirtualHost setup for Port 80, you’ll also need to define one for Port 443, else SSL won’t work as expected.

RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
  • Hi @jtittle
    Thanks for the response! I should have mentioned that yes, I do have another virtual host block for port 443. The code you provided worked great, although the ultimate issue here was that i was an idiot and misspelled the serverName.

    • @sundanlotion

      It happens. I’ve made my fair share of mistakes and misspelled plenty of things when coding or configuring. That’s what happens during 24-72 hour sessions of either non-stop coding or server configurations :-).

      Glad that line worked and glad you caught the other issue as well!