Apache2 重写、重定向

刘大尉 于 2023-05-16 发布

SSL - 开启SSL

Enabling SSL

To enable SSL, type (as user root):

a2ensite default-ssl
a2enmod ssl

If you want to use self-signed certificates, you should install the ssl-cert package (see below). Otherwise, just adjust the SSLCertificateKeyFile and SSLCertificateFile directives in ‘/etc/apache2/sites-available/default-ssl.conf’ to point to your SSL certificate. Then restart apache:

service apache2 restart

The SSL key file should only be readable by root; the certificate file may be globally readable. These files are read by the Apache parent process which runs as root, and it is therefore not necessary to make the files readable by the www-data user.

开启 rewrite

$ a2enmod rewrite

开启重写后可以配置,Wordpress 的固定链接 并且 设置 80 端口的 全部流量重定向 到 443 端口,网站实现 全 Https 访问。

修改 sites-enable/ 下的 000-default.conf.

RewriteEngine on #启动rewrite引擎
RewriteCond %{SERVER_PORT} 80 #rewrite的条件是访问的服务器端口是80端口
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R,L] #规则

若要开启 WordPress 固定链接的调整,在 apache2.conf 下修改两处地方。

修改他们两处为 All. 即可解决固定链接的重写和覆写。AllowwOverride None 则会完全忽略 .htaccess 文件,自然项目中所定义的 .htaccess 文件就不会生效了。