【发布时间】:2014-11-07 02:18:21
【问题描述】:
我正在尝试在 Ubuntu 14.04 (Apache 2.4.7) 中使用 .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/apache2.conf如下
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
AccessFileName .htaccess
但是.htaccess 文件仍未加载。如果我在.htaccess 文件中添加乱码,一切仍然正常。 .htaccess 位于/var/www/。
这是apache2ctl -S的输出
VirtualHost configuration:
*:80 is a NameVirtualHost
default server 127.0.1.1 (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost 127.0.1.1 (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost edmhunters.com (/etc/apache2/sites-enabled/edmhunters.com.conf:1)
alias www.edmhunters.com
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
我错过了什么?
这是apachectl -t -D DUMP_MODULES的输出
Loaded Modules:
core_module (static)
so_module (static)
watchdog_module (static)
http_module (static)
log_config_module (static)
logio_module (static)
version_module (static)
unixd_module (static)
access_compat_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_core_module (shared)
authn_file_module (shared)
authz_core_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
deflate_module (shared)
dir_module (shared)
env_module (shared)
expires_module (shared)
filter_module (shared)
headers_module (shared)
mime_module (shared)
mpm_event_module (shared)
negotiation_module (shared)
rewrite_module (shared)
setenvif_module (shared)
status_module (shared)
wsgi_module (shared)
【问题讨论】:
-
您是否尝试在域“edmhunters.com”上使用它,如果是,您需要检查该域的
edmhunters.com.conf,并将其 DocumentRoot 视为.htaccess应该在的位置就是这样。 -
@Prix 我的
edmhunters.com.conf中有DocumentRoot /home/jaskaran/edmhunters。我在那里复制了.htaccess文件,但现在出现 500 错误。我已将 error.log 添加到我的问题中。 -
评论
.htaccess的所有行并逐条取消评论,看看是否有任何问题导致问题以及是什么 -
@Prix 为
/etc/apache2/apache2.conf中的根目录添加AllowOverride All消除了500 错误。由于我的文档根目录与/var/www/位于不同的位置,我应该这样做吗?我还看到.htaccess文件现在正在加载,但压缩和 Cache-Control 标头仍然无法正常工作。 -
您是否在服务器中启用了模块以允许它们工作?是的,最好你有每个
VirtualHost的目录选项。
标签: apache .htaccess ubuntu ubuntu-14.04 apache2.4