Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Adam Menczykowski
Adam Menczykowski
2,319 Points

.htaccess question: force non-www and https

Hi guys!

I have a htaccess issue in that I can force non-www but I cannot force https on my server. Here is the .htaccess file at the moment:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

If anybody could advise how to force non-www AND https on all URL's from menchliving.com I would appreciate it!

5 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Adam,

We can force SSL by simply checking if the HTTPS header is set to off and if it is force the request to bounce back to the server using the HTTPS protocol, that's pretty much it.

The only other thing I'll mention is it's better to everything together to prevent issues when WordPress is making changes to the .htaccess file.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Hope that helps.

Adam Menczykowski
Adam Menczykowski
2,319 Points

Thanks for taking the time to answer this.

However, I get a 300 internal server error when using your code. Specifically, If I try to access just menchliving.com or www.menchliving.com without specifying https:// in the URL, I get this:

Found

The document has moved here. (the 'here' link doesn't work)

Additionally, a 500 Internal Server Error error :was encountered while trying to use an ErrorDocument to handle the request.

Apache/2.2.22 (Debian) Server at menchliving.com Port 80

However, when I remove the lines:

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

... typing in menchliving.com of course does not force ssl but does work.

Adam Menczykowski
Adam Menczykowski
2,319 Points

I wonder if this is anything to do with my virtual host setup in apache?

Adam Menczykowski
Adam Menczykowski
2,319 Points

Hey, I managed to get things working using the following code:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

However I still have one issue. If I direct my browser to https://www.menchliving.com It doesn't remove the www

Any tips at all?

Thanks