服务器没有读取htaccess文件并且重定向不起作用

时间:2018-02-27 16:02:01

标签: apache .htaccess mod-rewrite virtualhost lets-encrypt

我的网站没有读取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]

我尝试将其添加到虚拟主机,但它仍未重定向。

1 个答案:

答案 0 :(得分:0)

使用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指令。所以我假设发生的事情是内容是重复的,然后我只改变了原来的内容。