between 0 and 1

[SSL, Apache] http -> https & www to non-www setting 본문

Software Development Engineering/Dev.Info.

[SSL, Apache] http -> https & www to non-www setting

devxpert.yoon 2021. 2. 10. 00:37
728x90
반응형

 

Step 1. create ssl certificate

$ letsencrypt

 

Step 2. create .htaccess file on webroot and save below contents.

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

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

 

Step 3. set vhost.conf

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/www
    ErrorLog /var/www/example.com/log/error.log
    CustomLog /var/www/example.com/log/requests.log combined

    #AllowOverride All

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.example.com [OR]
    RewriteCond %{SERVER_NAME} =example.com
    RewriteRule ^ https://issuemore.com%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Rewrite engine will redirect all http access to https

 

 

Step 4. set vhost-le-ssl.conf

<IfModule mod_ssl.c>

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/
    ErrorLog /var/www/example.com/log/error.log
    CustomLog /var/www/example.com/log/requests.log combined


    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.example.com
    RewriteRule ^ https://example.com%{REQUEST_URI} [END,NE,R=permanent]
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>

</IfModule>

 

 

Step 5. restart httpd

$ sudo systemctl restart httpd
728x90
반응형