How to fix the error “Invalid command ‘RewriteEngine'”

I noticed one error after installing FreePBX:

Invalid command ‘RewriteEngine’, perhaps misspelled or defined by a module not included in the server configuration

To solve it, you need to activate the rewrite module in apache2:

sudo a2enmod rewrite

And restart apache2 to apply the changes:

sudo service apache2 restart

Done.

Access Control Apache2

Access control Apache2 version 2.4 slightly different from 2.2, for example, to allow access to all, in version 2.4 you need to specify:

Require all granted

Allow access to the specified IP addresses:

Require local
Require ip 192.168.56.1 192.168.22.10

Allow all but the specified IP:

Require all granted
Require not ip 192.168.56.1

Allow the specified host:

Require host example.com

Prohibit all:

Require all denied

And in the version of Apache2 2.2, permit everyone access like this:

Order allow,deny
Allow from all

Prohibit all:

Order deny,allow
Deny from all

Allow access by specified IP:

Order allow,deny
Allow from 192.168.56.1 192.168.22.10

Allow the specified host:

Order Deny,Allow
Deny from all
Allow from example.com

After the changes in the Apache2 configuration, a reboot is required (if the changes were in the .htaccess file, then the reboot is not required):

sudo service apache2 restart

See also:
Using .htaccess

Redirecting requests to SSL

I will describe several options for redirecting requests from HTTP to HTTPS, the first and second methods are the most reliable:

1) Across virtual hosts.
In the site configuration, add the line “Redirect”, for example, when an SSL certificate was installed on the site and you need to redirect all requests to HTTPS:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName ixnfo.com
   ServerAlias www.ixnfo.com
   Redirect / https://ixnfo.com/
</VirtualHost>

<VirtualHost *:443>
   ServerName ixnfo.com
   ServerAlias www.ixnfo.com
   DocumentRoot /var/www/html
   SSLEngine On
   ...
</VirtualHost>

If you want to redirect only some requests:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName ixnfo.com
   ServerAlias www.ixnfo.com
   Redirect /forum https://forum.ixnfo.com/
</VirtualHost>

<VirtualHost *:443>
   ServerName ixnfo.com
   ServerAlias www.ixnfo.com
   DocumentRoot /var/www/html
   SSLEngine On
   ...
</VirtualHost>

2) Redirecting using .htaccess.
Similarly, as in the first version, put the .htaccess file in the desired directory of the site and add a line to it (so that the web server takes into account the .htaccess file, you need to specify the option AllowOverride All in the site configuration above):

Redirect /forum https://forum.ixnfo.com/

3) And not the recommended way, using mod_rewrite, the content should be added to the .htaccess file:

# Enabling the Rewrite function
RewriteEngine On
# Verify that the connection is not HTTPS
RewriteCond %{HTTPS} !=on
# We are sending to the same place, but already HTTPS:
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

Another example:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^ixnfo\.com$ [OR]
RewriteCond %{SERVER_NAME} ^www\.ixnfo\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
</IfModule>

To allow some pages to open via http and https, add the following in the top example:

RewriteCond %{REQUEST_URI} !^/dir/
RewriteCond %{REQUEST_URI} !^/dir/file.php

See also:
Using .htaccess
How to configure SSL and HTTPS for WordPress
Installing Certbot in Ubuntu