Skip to content

[apache] 強制網站及 wordpress 使用 https 私人連線

Published: at 12:22 PM (1 min read)

安裝完了 letscrypt 後,當然要讓自己的網站都強制使用 https 較安全的連線囉!

  1. 首先要先開啟 rewrite module sudo a2enmod rewrite

  2. 打開 /etc/apache2/apache2.conf ,加入以下程式碼

    <Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    RewriteEngine on
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
    </Directory>
  3. 若為 wordpress ,則要進入wordpress 目錄後複寫 .htaccess

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
    </IfModule>
    # END WordPress
    
    
  4. 重新啟動 apache sudo service apache2 restart

  5. 完成囉!

參考資料:

http://blog.changyy.org/2013/02/apache-web-server-https-ubuntu-1204.html