Yin*_*ang 1 apache .htaccess mod-rewrite ubuntu-14.04
我正在尝试.htaccess在我的 Apache 服务器上使用该文件。
这是我的.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
Run Code Online (Sandbox Code Playgroud)
我启用了 mod_rewrite 使用 a2enmod rewrite
我遵循的教程告诉我编辑文件,/etc/apache2/sites-available/default但该文件夹中没有同名default的文件。000-default.conf在同一条路径上有一个替代。但是那个文件没有那个部分
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Run Code Online (Sandbox Code Playgroud)
我应该编辑。
这是内容 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
Run Code Online (Sandbox Code Playgroud)
但是/etc/apache2/apache2.conf有完全相同的部分。所以我把AllowOverride None那边换成了AllowOverride All. 之后我重新启动了服务器。
但是,该.htaccess文件仍未加载。如果我向.htaccess文件中添加乱码,一切仍然正常,这意味着它没有被加载。
我在这里错过了什么?
小智 5
首先,文件名并不重要。000-default.conf,虽然不是很常见,但对我来说似乎很好。
修改站点配置
提供 .htaccess 文件时,您必须考虑两件主要事情:
AccesFileName .htaccess
AllowOverride All
Run Code Online (Sandbox Code Playgroud)
正如apache2 doc 中所说,您必须AllowOverride在Directory部分中声明。这就是为什么在主配置文件中设置它时它不起作用的原因。
我建议您将这 4 行粘贴到Virtualhost部分的/etc/apache2/000-default.conf 中,它应该可以工作:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
Run Code Online (Sandbox Code Playgroud)
(假设你的根目录是/var/www)
您不必将AccessFileName设置为默认值.htaccess。
如果它不起作用并且您有 1 个以上的文件 /etc/apache2/sites-available
Apache 可能会使用另一个配置而不是 000-default.conf。只需检查其中哪些符号链接到/etc/apache2/sites-enabled。如果仍然超过 1 个,您可能需要禁用所有其他启用的站点以确保安全。
如果它仍然不起作用,只需检查文件所有权和权限。