Force SSL and WWW on apache2 for raspbian - apache

When I use the mod_rewrite and mod_ssl concurrently below, the resulting domain is: https://www.domain.com// I find this extra '/' annoying and I would like to get rid of it. Is the problem the way I code it or should I consider using .htaccess instead?
000-default:
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.fusioncodes\.com [NC]
RewriteRule ^(.*)$ https://www.fusioncodes.com/$1 [R=301,L]
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://www.%{HTTP_HOST}/$1 [NC,R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
SSLEngine On
SSLCertificateFile ~server.crt
SSLCertificateKeyFile ~server.key
..... More SSL Stuff
</VirtualHost>

Change the 3rd line (RewriteRule) with that:
RewriteRule ^/?(.*)$ https://www.fusioncodes.com/$1 [R=301,L]

Refer here RewriteRule ^/?(.*) https://www.%{HTTP_HOST}/$1 [NC,R=301,L]

Related

Combining Rewrite Rules - http to https AND all links to one file while maintaining clean URL

Over the past few days, I have read quite a few forums and StackOverflow answers on rewriting URLs in apache.conf and converting http to https using rewrites. Thanks to StackOverflow, I have figured out how to do both separately. Unfortunately, I haven't found a solution that can combine the two while allowing me to keep my clean URL appearance AND while letting me pass a variable to page in a hidden manner.
Here is the current directory part of my apache.conf file:
<Directory /home/tim/example>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sort-url.php?rt=$1
</Directory>
I currently pass of "https://example.com/CA/Los-Angeles" to a file called "sort-url.php", where I get the "rt" variable, explode it into an array using the "/", and then use the variables like $route_array[0] for "CA" and $route_array[1] = "Los-Angeles". Right now, the user will never see "sort-url.php", so my URLs always look clean. I want to keep this same effect, but convert http to https for all sites.
I found this code to convert to https if I substitute this rewrite rule for the other rewrite rule:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}/sort-url.php?rt=$1 [R,L]
But, this causes "sort-url.php?rt=CA/Los-Angeles" to show up in the URL. If I get rid of the "sort-url.php in the rewrite rule like this:
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]
It won't know to route all requests to the sort-url.php file, but it will look like the clean link I want with https.
Am I going about it the wrong way? Is it possible to combine the two rules while maintaining a clean URL?
Thank you in advance,
Tim
ADDITION - Here are my virtual hosts:
<VirtualHost XXX.XXX.XX.XXX:12345>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost XXX.XXX.XX.XXX:80>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost XXX.XXX.XX.XXX:443>
ServerName example.com
DocumentRoot /home/tim/example
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/certs/example.key
SSLCertificateChainFile /etc/ssl/certs/sf_bundle.crt
</VirtualHost>
Enable .htaccess if not already enabled.
Try this code in your DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sort-url.php?rt=$1 [L,QSA]
I suggest suppressing Directory clause (COMMENT), and this in vhosts:
<VirtualHost XXX.XXX.XX.XXX:80>
ServerName example.com
ServerAlias *.example.com
DocumentRoot /home/tim/example
Redirect / https://example.com/
</VirtualHost>
<VirtualHost XXX.XXX.XX.XXX:443>
ServerName example.com
ServerAlias *.example.com
DocumentRoot /home/tim/example
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/certs/example.key
SSLCertificateChainFile /etc/ssl/certs/sf_bundle.crt
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /sort-url.php?rt=$1 [L,QSA]
</VirtualHost>
Ok, it appears my load balancer configuration (SSL termination on LB, port 80, etc.) was throwing me for a loop and "X-Forwarded-Proto" was the answer I needed. Here is my updated directory part:
<Directory /home/tim/example>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sort-url.php?rt=$1 [L,QSA]
</Directory>

How to redirect from www.domain.com to domain.com?

I am using the following code in my httpd.conf for redirections:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
But, it ends up redirecting to www.domain.com/domain.com//domain.com//domain.com
I want all the following URLs to redirect to domain.com:
http://domain.com
http://www.domain.com
www.domain.com
This should do it. The problem is that it looks pretty much like what you already have. When you type http://domain.com in your browser, does it work? Or does it redirect to somewhere else?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteRule ^(.*)$ http://domain.com$1 [R=permanent,L]
http://www.webweaver.nu/html-tips/web-redirection.shtml suggests
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteEngine On
# Redirect ANY non-www request to https and www
RewriteCond %{HTTP_HOST} !^(www.\.domain\.com)?$
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
# Redirect ANY HTTP request to https and www
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
for SSL Virtual host use the following:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com
RewriteRule ^(.*)$ https://yourdomain.com$1 [R=301,L]
Full Example below:
<VirtualHost *:443>
ServerAdmin youname#yourdomain.com
DocumentRoot "/var/www/html/yourdomain"
ServerName yourdomain.com
ServerAlias www.yourdomain.com *.yourdomain.com
ErrorLog "logs/yourdomain-error_log"
CustomLog "logs/yourdomain-access_log" common
ErrorDocument 404 /index.php
<Directory "/var/www/html/yourdomain">
Options FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/yourdomain/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/yourdomain/chain.pem
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com
RewriteRule ^(.*)$ https://yourdomain.com$1 [R=301,L]
</VirtualHost>

Wild card SSL certificate - redirect http to https

I have a single HTTPS virtual host for all of my websites. I am trying to redirect subsite2.server.co.uk from http to https.
Here is my HTTPS virtual host (I have shortened it for brevity):
<VirtualHost _default_:443>
ServerName *.server.co.uk
ServerAdmin imran.azad#server.co.uk
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.subsite1.server.co.uk$ [NC]
RewriteRule ^(.*)$ http://localhost:8080/VirtualHostBase/https
/www.subsite1.co.uk:443/Plone/VirtualHostRoot/$1 [L,P]
RewriteCond %{HTTP_HOST} ^subsite2.server.co.uk$ [NC]
RewriteRule ^(.*)$ http://subsite2.server.co.uk/$1 [P]
Here is my HTTP virtual host:
<VirtualHost *:80>
ServerName subsite2.server.co.uk
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Alias /static-dir/ x:/.....
<Directory x:/.....>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / x:/......
<Directory x:/......>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I am using the following rewrite condition and rule to do the redirection:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
However the problem I'm having is that it appears as though the server is in a redirect loop. How can I get around this problem?
Here is an error from firefox:
The page isn't redirecting properly
Mine is working using something similar, my guess is, try using your regex match instead REQUEST_URI
RewriteEngine On
RewriteCond %{HTTP_HOST} =myhost.com
RewriteCond %{SERVER_PORT} !443
RewriteRule /(.*) https://%{SERVER_NAME}/$1 [R,L]

Problem with Apache rewrite rules. I want to redirect .com traffic to .co.uk

This is my current vhost.conf file:
<VirtualHost *:80>
DocumentRoot /data/sites/mysite
ServerName mysite.co.uk
ServerAlias www.mysite.co.uk
ServerName mysite.com
ServerAlias www.mysite.com
RewriteEngine On
RewriteOptions inherit
RewriteCond %{HTTP_HOST} !^www\.mysite\.co\.uk|^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} !^mysite\.co\.uk|^mysite\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.mysite.co.uk/$1 [L,R]
CustomLog /var/log/httpd/mysite.log combined
</VirtualHost>
But I cannot get it to work properly. www.mysite.com is showing the mysite.co.uk website but the .com domain is still showing in the browser. Also if I go to www.mysite.com/myfiles it cannot find the folder that works on www.mysite.co.uk/myfiles
Any ideas?
Try this on the .com domain to redirect all requests to the co.uk
RewriteEngine On
RewriteRule ^(.*) http://yoursite.co.uk/$1 [R=301,L]

apache redirect from non www to www

I have a website that doesn't seem to redirect from non-www to www.
My Apache configuration is as follows:
RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
What am I missing?
Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
# real server configuration
</VirtualHost>
And then you'll have another <VirtualHost> section with ServerName www.example.com for your real server configuration. Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won't work (when in fact it does).
http://example.com/subdir/?lold=13666 => http://www.example.com/subdir/?lold=13666
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<VirtualHost *:80>
ServerAlias example.com
RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>
To remove www from your URL website use this code in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
To force www in your website URL use this code on .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^YourSite.com$
RewriteRule ^(.*)$ http://www.yourSite.com/$1 [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]
Where YourSite.com must be replaced with your URL.
<VirtualHost *:80>
DocumentRoot "what/ever/root/to/source"
ServerName www.example.com
<Directory "what/ever/root/to/source">
Options FollowSymLinks MultiViews Includes ExecCGI
AllowOverride All
Order allow,deny
allow from all
<What Ever Rules You Need.>
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
This is what happens with the code above. The first virtual host block checks if the request is www.example.com and runs your website in that directory.
Failing which, it comes to the second virtual host section. Here anything other than www.example.com is redirected to www.example.com.
The order here matters. If you add the second virtualhost directive first, it will cause a redirect loop.
This solution will redirect any request to your domain, to www.yourdomain.com.
Cheers!
This is similar to many of the other suggestions with a couple enhancements:
No need to hardcode the domain (works with vhosts that accept multiple domains or between environments)
Preserves the scheme (http/https) and ignores the effects of previous %{REQUEST_URI} rules.
The path portion not affected by previous RewriteRules like %{REQUEST_URI} is.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}/$1 [R=301,L]
Redirection code for both non-www => www and opposite www => non-www. No hardcoding domains and schemes in .htaccess file. So origin domain and http/https version will be preserved.
APACHE 2.4 AND NEWER
NON-WWW => WWW:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WWW => NON-WWW:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]
Note: not working on Apache 2.2 where %{REQUEST_SCHEME} is not available. For compatibility with Apache 2.2 use code below or replace %{REQUEST_SCHEME} with fixed http/https.
APACHE 2.2 AND NEWER
NON-WWW => WWW:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
... or shorter version ...
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|offs
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WWW => NON-WWW:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
... shorter version not possible because %N is available only from last RewriteCond ...
RewriteCond %{HTTP_HOST} ^!example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This starts with the HTTP_HOST variable, which contains just the domain name portion of the incoming URL (example.com). Assuming the domain name does not contain a www. and matches your domain name exactly, then the RewriteRule comes into play. The pattern ^(.*)$ will match everything in the REQUEST_URI, which is the resource requested in the HTTP request (foo/blah/index.html). It stores this in a back reference, which is then used to rewrite the URL with the new domain name (one that starts with www).
[NC] indicates case-insensitive pattern matching, [R=301] indicates an external redirect using code 301 (resource moved permanently), and [L] stops all further rewriting, and redirects immediately.
If you are using Apache 2.4 ,without the need to enable the rewrite apache module you can use something like this:
# non-www to www
<If "%{HTTP_HOST} = 'domain.com'">
Redirect 301 "/" "http://www.domain.com/"
</If>
I ran this...
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.*$ [NC]
RewriteRule ^/.+www\/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I need this to be universal for 25+ domains on our new server, so this directive is in my virtual.conf file in a <Directory> tag. (dir is parent to all docroots)
I had to do a bit of a hack on the rewrite rule though, as the full docroot was being carried through on the pattern match, despite what http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html says about it only being stuff after the host and port.
Redirect domain.tld to www.
The following lines can be added either in Apache directives or in .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Other sudomains are still working.
No need to adjust the lines. just copy/paste them at the right place.
Don't forget to apply the apache changes if you modify the vhost.
(based on the default Drupal7 .htaccess but should work in many cases)
<VirtualHost *:80>
ServerAlias example.com
RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>
This will redirect not only the domain name but also the inner
pages.like...
example.com/abcd.html ==> www.example.com/abcd.html example.com/ab/cd.html?ef=gh ==> www.example.com/ab/cd.html?ef=gh
This is simple!
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*) http://www.example.com$1 [R=301]
Do not always use Redirect permanent (or why it may cause issues in future)
If there is a chance that you will add subdomains later, do not use redirect permanent.
Because if a client has used a subdomain that wasn't registred as VirtualHost he may also never reach this subdomain even when it is registred later.
redirect permanent sends an HTTP 301 Moved Permanently to the client (browser) and a lot of them cache this response for ever (until cache is cleared [manually]). So using that subdomain will always autoredirect to www.*** without requesting the server again.
see How long do browsers cache HTTP 301s?
So just use Redirect
<VirtualHost *:80>
ServerName example.com
Redirect / http://www.example.com/
</VirtualHost>
Apache.org - When not to use mod_rewrite
Apache.org - Canonical Hostnames
To 301 redirect all requests made directly to the domain to www you can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^([^.]+\.[^.]+){2,}$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The benefit of this is that this will work if you have any valid subdomains, e.g.
example.com REDIRECTED TO www.example.com
foo.example.com NO REDIRECT
bar.example.com NO REDIRECT
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
check this perfect work
-If you host multiple domain names (Optional)
-If all those domain names are using https (as they should)
-if you want all those domain names to use www dot domainName
This will avoid doble redirection (http://non www to http://www and then to https://www)
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ https://www.%1$1 [R=301,L]
</VirtualHost>
And
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
You should change the redirection code 301 to the most convenient one
If you want to load only the https version of www, use the below configurations in apache virtual host file. all these can have in a single file.
redirecting all http to https of www:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
redirecting https non-www to https www:
<VirtualHost *:443>
ServerName example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
real server configuration
<VirtualHost *:443>
ServerAdmin hostmaster#example.com
DocumentRoot "/path/to/your/sites/.htaccess-file-folder"
SetEnv APPLICATION_ENV "production"
<Directory "/path/to/your/sites/.htaccess-file-folder">
Options Indexes FollowSymLinks
DirectoryIndex index.php index.html
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ServerName www.example.com
SSLEngine ON
SSLCertificateFile "/path/to/your/example.cert.pem"
SSLCertificateKeyFile "/path/to/your/example.key.pem"
ErrorLog /path/to/your/example.com-error.log
CustomLog /path/to/your/example.com-access.log combined
#Your other configurations if you have
</VirtualHost>
If using the above solution of two <VirtualHost *:80> blocks with different ServerNames...
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
</VirtualHost>
... then you must set NameVirtualHost On as well.
If you don't do this, Apache doesn't allow itself to use the different ServerNames to distinguish the blocks, so you get this error message:
[warn] _default_ VirtualHost overlap on port 80, the first has precedence
...and either no redirection happens, or you have an infinite redirection loop, depending on which block you put first.
I've just have a same problem.
But solved with this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This rule redirecting non-www to www.
And this rule to redirecting www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]
Refer from http://dense13.com/blog/2008/02/27/redirecting-non-www-to-www-with-htaccess/
I had a similar task on a WP Multisite, where the redirection rule had to be generic (for any given domain I'd add to the network). I solved first adding a wildcard to the domain (parked domain). Note the . after .com.
CNAME * domain.com.
And then I added the following lines to the .htaccess file at the root of my multisite. I guess it'd work for any site.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Hopefully this will help.
ps. If you'd like to redirect from not www to www, change the last line into
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
This works for me:
RewriteCond %{HTTP_HOST} ^(?!www.domain.com).*$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]
I use the look-ahead pattern (?!www.domain.com) to exclude the www subdomain when redirecting all domains to the www subdomain in order to avoid an infinite redirect loop in Apache.
The code I use is:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
This is my own site's configuration, and works like a charm.
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin#domain.com
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /var/www/html/domain
<Directory /var/www/html/domain/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
</VirtualHost>
</IfModule>
I found it easier (and more usefull) to use ServerAlias when using multiple vhosts.
<VirtualHost x.x.x.x:80>
ServerName www.example.com
ServerAlias example.com
....
</VirtualHost>
This also works with https vhosts.