服务器不读取 htaccess 文件和重定向不工作
Server not reading htaccess file and redirect not working
我的网站没有读取 htaccess 文件。即使我在顶部添加 Deny from all
,网站也能正常加载。
权限为 644。我在服务器上有多个站点 运行,因此 Apache 设置正确。
这是我的虚拟主机配置:
<VirtualHost 12.34.56.78:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
<Directory /srv/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
这是我添加到 .htaccess 文件以将 www 重定向到非 www 的内容:
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
我也试过将它添加到虚拟主机,但它仍然没有重定向。
使用 Let's Encrypt 颁发 SSL 证书后,我注意到它为端口 443 创建了一个重复的 conf 文件:
/etc/apache2/sites-available/example.com.conf
/etc/apache2/sites-available/example.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost 12.34.56.78:443>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
<Directory /srv/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
在 LE 之前,原来的 example.com.conf
没有 AllowOverride All
指令。所以我假设发生的事情是内容重复,然后我只更改了原来的内容。
我的网站没有读取 htaccess 文件。即使我在顶部添加 Deny from all
,网站也能正常加载。
权限为 644。我在服务器上有多个站点 运行,因此 Apache 设置正确。
这是我的虚拟主机配置:
<VirtualHost 12.34.56.78:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
<Directory /srv/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
这是我添加到 .htaccess 文件以将 www 重定向到非 www 的内容:
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
我也试过将它添加到虚拟主机,但它仍然没有重定向。
使用 Let's Encrypt 颁发 SSL 证书后,我注意到它为端口 443 创建了一个重复的 conf 文件:
/etc/apache2/sites-available/example.com.conf
/etc/apache2/sites-available/example.com-le-ssl.conf
<IfModule mod_ssl.c> <VirtualHost 12.34.56.78:443> ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /srv/www/example.com/public_html/ <Directory /srv/www/example.com/public_html/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog /srv/www/example.com/logs/error.log CustomLog /srv/www/example.com/logs/access.log combined Include /etc/letsencrypt/options-ssl-apache.conf SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem Include /etc/letsencrypt/options-ssl-apache.conf </VirtualHost> </IfModule>
在 LE 之前,原来的 example.com.conf
没有 AllowOverride All
指令。所以我假设发生的事情是内容重复,然后我只更改了原来的内容。