Redirect to www when using SSL

Hi,

Have a strange problem. Using certificate for a few domains, but one of them has an issue with the Apache configuration.

This is the apache configuration for the virtual host:````
ServerAdmin support@domain.tld
ServerName www.domain.tld
ServerAlias domain.tld

RewriteEngine on
RewriteCond %{HTTPHOST} !^$ [OR] RewriteCond %{HTTPHOST} !^www. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTPHOST}%{REQUESTURI} [L,R=301]

ServerAdmin support@domain.tld
ServerName www.domain.tld
ServerAlias domain.tld

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^ https://www.domain.tld%{REQUEST_URI} [R=301,L]

DocumentRoot /var/www/domain.tld/public_html/
ErrorLog /var/www/domain.tld/logs/error.log
CustomLog /var/www/domain.tld/logs/access.log combined

SSLEngine on
SSLCertificateChainFile /etc/ssl/AlphaSSLRoot-Bundle.crt
SSLCertificateFile /etc/ssl/localcerts/www.domain.tld.crt
SSLCertificateKeyFile /etc/ssl/localcerts/www.domain.tld.key
````

http://domain.tld redirects OK to https://www.domain.tld

http://www.domain.tld redirects OK to https://www.domain.tld

But https://domain.tld does not work at all, just getting "Server not found". It should of course redirect to https://www.domain.tld. Since http://domain.tld redirects okay above, it shouldn't be a DNS issue, but something in the Apache config.

Any help on this is appreciated! Thanks!

6 Replies

Could it be that you don't have this line in your https section even though it's in your http section?

RewriteCond %{HTTP_HOST} !^$ [OR]

I'm no htaccess expert, but I've always used that line as part of the conditions when forcing 'www'.

Hi,

What exactly is that row doing? I tried to put it first (after RewriteEngine on) but then I'm getting "Page is redirecting the wrong way" and it stops loading. Any other suggestions?

Webkungen,

I'm not sure what it does, but I've always needed it for redirecting to 'www'.

James

The best reference I know of for .htaccess redirects is http://www.askapache.com/category/htaccess/

I have had considerable good fortune following visits to that site. There is considerable quality information there regarding all of apache. Hope that site is found to be useful. 8)

about 1/2 way down that page is this ~~[http://www.askapache.com/category/htaccess/modrewrite.html" target="blank">](http://www.askapache.com/category/htacc … write.html">http://www.askapache.com/category/htaccess/mod_rewrite.html](

(added that specific link)

Hi,

Thanks for the link, but cannot find what I'm looking for. The force www is working fine for the Non-SSL (port 80) part, the problem is that https:// won't redirect as it should.

this is a working example… for doing it the 'other way'… forcing https and no 'www'.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#Second Force HTTPS via redirect/rewrite
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

I think if you use mine as a template you will get working as you desire…. as yours stands apache has no way to know that 'https://domain.tld' exists… it seems to be refering it back into a loop so it tells you it failed.

take note of the "."[dot]

try this code for your line that looks similiar:(the last line)

RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct