Question

Rewrite Rules for multiple subdomains and https

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

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>
	ServerAdmin ####@gmail.com
	ServerName thedenveradeventure.com
	ServerAlias www.thedenveradventure.com
	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
	</Directory>
	
	RewriteEngine On
	RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
	RewriteCond %{HTTP_HOST} !^dev.thedenveradventure.com$
	RewriteCond %{HTTP_HOST} !^www\.[NC]
	RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

</VirtualHost>



Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

@sundanlotion

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]

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel