Rewrite rules will help you with this. It’s very powerful utility, you do almost all URL manipulations with it.
We will added needed rules to Apache config. If you use default configuration, it’s stored in file called 000-default-le-ssl.conf
. Open it with text editor:
- sudo nano /etc/apache2/sites-available/000-default-le-ssl.conf
By default, this is content of file:
GNU nano - /etc/apache2/sites-available/000-default-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
ServerName example.com
ServerAlias example.com
</VirtualHost>
</IfModule>
You need to add following rules:
Rewrite rules
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^/(.*) https://www.example.com/$1 [L,R]
First rule - RewriteEngine On
, enables rewrite engine for you.
Second one is condition, if it’s you are not accessing via www.example.com
, RewriteRule
will execute.
Third one is rule, it rewrites your URL to correct one - www.example.com
When you add it, your file will look like:
GNU nano - /etc/apache2/sites-available/000-default-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^/(.*) https://www.example.com/$1 [L,R]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
ServerName example.com
ServerAlias example.com
</VirtualHost>
</IfModule>
It requires you to have rewrite
module enabled. To make sure, execute this:
To make this changes in effect - you’ll have to restart Apache:
- sudo systemctl restart apache2
This can be done via .htaccess
to, but as you have access to config files, you can use config files. :)
This is done for @ayan, now let’s go on @newbie part. =)
This depends on your setup. If you followed How To Secure Nginx with Let’s Encrypt on Ubuntu 16.04, you have 2 server blocks. One to redirect, and one for HTTPS.
In that case, you just need to add www
to return
. Let’s assume you have redirect block like this one:
/etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
If you change https://$server_name$request_uri;
to https://www.$server_name$request_uri;
, you’ll get desired result.
And block will look as this:
/etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 301 https://www.$server_name$request_uri;
}
i also have the same question but for Nginx. and dont want 2 rewrite rules. one single rule.