Apache服务器忽略了`.htaccess`文件

42
我试图在我的测试环境上让一个网站运行起来,但不知何故它没有工作。我可以加载正常的首页,但当我想访问 /page/test 时,它会抛出一个错误,说该页面不存在。我的日志显示:

文件不存在:/home/page_url/www/page

事实上,这是正确的,但它应该去到我的 Page 控制器,然后加载 test 方法。

我的 .htaccess 文件看起来像:

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* /$0 [L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

我的虚拟主机配置如下:

<VirtualHost *:80>
    ServerName page_url
    Include /etc/apache2/vhosts.d/vhco.include
    DocumentRoot "/home/page_url/www/"

    # Logging
    CustomLog /var/log/apache2/access_log common
    ErrorLog /var/log/apache2/error_log

    # This should be changed to whatever you set DocumentRoot to.
    <Directory "/home/page_url/www/">
        # Possible values for the Options directive are "None", "All",
        # or any combination of:
        #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
        #
        # Note that "MultiViews" must be named *explicitly* --- "Options All"
        # doesn't give it to you.
        #
        # The Options directive is both complicated and important.  Please see
        # http://httpd.apache.org/docs/2.2/mod/core.html#options
        # for more information.
        Options Indexes FollowSymLinks

        # AllowOverride controls what directives may be placed in .htaccess files.
        # It can be "All", "None", or any combination of the keywords:
        #   Options FileInfo AuthConfig Limit
        AllowOverride All

        # Controls who can get stuff from this server.
        Order allow,deny
        Allow from All 
    </Directory>

    <IfModule alias_module>
        # Redirect: Allows you to tell clients about documents that used to
        # exist in your server's namespace, but do not anymore. The client
        # will make a new request for the document at its new location.
        # Example:
        #   Redirect permanent /foo http://www.example.com/bar

        # Alias: Maps web paths into filesystem paths and is used to
        # access content that does not live under the DocumentRoot.
        # Example:
        #   Alias /webpath /full/filesystem/path
        #
        # If you include a trailing / on /webpath then the server will
        # require it to be present in the URL.  You will also likely
        # need to provide a <Directory> section to allow access to
        # the filesystem path.

        # ScriptAlias: This controls which directories contain server scripts.
        # ScriptAliases are essentially the same as Aliases, except that
        # documents in the target directory are treated as applications and
        # run by the server when requested rather than as documents sent to the
        # client.  The same rules about trailing "/" apply to ScriptAlias
        # directives as to Alias.
        ScriptAlias /cgi-bin/ "/var/www/localhost/cgi-bin/"
    </IfModule>

    # "/var/www/localhost/cgi-bin" should be changed to whatever your ScriptAliased
    # CGI directory exists, if you have that configured.
    <Directory "/home/page_url/www/">
        AllowOverride None
        Options None
        Order allow,deny
        Allow from All
    </Directory>
</VirtualHost>

我正在使用 Gentoo。
欢迎提供任何帮助。
4个回答

83
<Directory "/home/page_url/www/">
    AllowOverride None

这个AllowOverride None命令禁止读取.htaccess文件,请参考手册

此外,请注意,.htaccess文件并没有什么神奇的地方,它们只是一个解决没有完全访问服务器配置的粗略方法。它们只是Apache配置的一部分。如果您可以完全访问服务器配置,应该将这样的内容放入vhost配置中,而不是.htaccess文件中。


2
请务必检查/etc/apache2/sites-available/etc/apache2/apache2.conf中的配置,确保AllowOverride None。如果不这样做,我在整个会议期间都无法显示一个可工作的网页版本! - Severo Raz
13
“AllowOverride All” 解决了这个问题。 - jim smith
为什么这应该在虚拟主机配置中?如果放在那里,您必须重新启动Apache,而.htaccess不需要重新启动任何内容。 - Altimus Prime
1
@AltimusPrime 这样做可以提高性能。 - Xavi Esteve

14

正如Jim所说,如果您完全拥有服务器访问权限,应该将所有内容放置在服务器配置文件中。

我来到这里是因为我认为我的服务器忽略了我的htaccess/server配置文件。然而,结果表明我已经禁用了mod_expires和mod_rewrite。在对这两个组件进行调查后,一切又恢复正常了。

您可以通过执行以下命令启用它们:

sudo a2enmod expires
sudo a2enmod rewrite

然后重新启动Apache

service apache2 restart

希望这能帮到某个人!


谢谢。仅使用mod rewrite是不够的,我必须添加expires。 - Jerome VDL

5
如果您的重写规则仍然无法正常工作,请记住一件事情:同时激活ModRewrite模块!在Ubuntu中默认情况下不会激活该模块。

请参考此处的其他答案以了解如何操作。

这是我的问题。我已经启用了模块,但我的 .htaccess 文件缺少 RewriteEngine on。这就是我从另一个配置中复制/粘贴时搞砸的后果! - vaindil
你能否把这个转化为完整的答案? - randy

0
在我的情况下,问题是.htaccess文件的权限。
解决方案:
sudo chown apache:apache .htaccess

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接