我正在尝试在我的 Apache 服务器上使用 .htaccess
文件。
这是我的 .htaccess
的样子
# 1 YEAR
<FilesMatch "\.(ico|svg|woff|eot|ttf)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
# 1 WEEK
<FilesMatch "\.(jpg|png|gif|css|js)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# Add correct content-type for fonts
AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType font/x-woff .woff
AddType image/svg+xml .svg
# Compress compressible fonts
AddOutputFilterByType DEFLATE font/ttf font/otf image/svg+xml
我使用 a2enmod rewrite
启用了 mod_rewrite
我遵循的教程告诉我编辑 /etc/apache2/sites-available/default
中的文件,但是没有名为 default
的文件那个文件夹。在同一路径中有一个 000-default.conf
。但是那个文件没有这个部分
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
我应该编辑它。
这是000-default.conf
的内容
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
但是 /etc/apache2/apache2.conf
有完全相同的部分。所以我将那里的 AllowOverride None
替换为 AllowOverride All
。之后我重新启动了服务器。
但是 .htaccess
文件仍然没有被加载。如果我向 .htaccess
文件添加乱码,一切仍然正常,这意味着它没有被加载。
我在这里错过了什么?
最佳答案
首先,文件名并不重要。 000-default.conf,虽然不是很常见,但对我来说似乎不错。
修改您的站点配置
在提供 .htaccess 文件时,您必须考虑两件主要的事情:
AccessFileName .htaccess
AllowOverride All
如 apache2 doc 中所述,您必须在 Directory 部分中声明 AllowOverride
。这就是为什么在主配置文件中设置它时它不起作用的原因。
我建议您将这 4 行粘贴到 /etc/apache2/000-default.conf 中的 Virtualhost 部分,它应该可以工作:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
(假设你的根目录是/var/www)
您不必设置 AccessFileName,因为默认为 .htaccess。
如果它不起作用并且您在 /etc/apache2/sites-available
中有超过 1 个文件
Apache 可能会使用除 000-default.conf 以外的其他配置。只需检查其中哪些符号链接(symbolic link)到 /etc/apache2/sites-enabled。如果仍然超过 1 个,您可能想要禁用所有其他已启用的站点以确保安全。
如果仍然不起作用,只需检查文件所有权和权限即可。
关于apache - .htaccess 被忽略 - Ubuntu 14.04,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25809132/