Apache 服务器忽略 .htaccess

Posted

技术标签:

【中文标题】Apache 服务器忽略 .htaccess【英文标题】:Apache server ignores .htaccess 【发布时间】:2011-07-09 19:26:30 【问题描述】:

我试图让一个网站在我的测试环境中运行,但不知何故它无法运行。我可以加载正常的索引页面,但是当我想访问 /page/test 时,它会抛出一个错误,指出该页面不存在。我的日志说:

File does not exist: /home/page_url/www/page

这实际上是正确的,但它应该转到我的页面控制器并加载测试方法。

我的 .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。

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:
<Directory "/home/page_url/www/">
    AllowOverride None

这个AllowOverride None 禁止读取.htaccess 文件。见the manual。

另外,请记住,.htaccess 文件并没有什么神奇之处。它们是无法完全访问服务器配置的粗略解决方法。它们只是一个 Apache 配置。如果您对服务器配置具有完全访问权限,则应该将此类内容放入 vhost 配置中,而不是 .htaccess 文件中。

【讨论】:

请务必检查/etc/apache2/sites-available/etc/apache2/apache2.conf 以获取带有AllowOverride None 的配置。不这样做使我无法在整个会议中显示我的网页的工作版本! AllowOverride All 修复了问题 为什么要在虚拟主机配置中?如果 apache 去那里,你必须重新启动它,而 .htaccess 不需要重新启动任何东西。【参考方案2】:

正如 Jim 所说,如果您可以完全访问您的服务器,那么您应该将所有内容都放在服务器配置文件中。

我到达这里是因为我认为我的服务器忽略了我自己的 htaccess/server 配置文件。然而,事实证明我禁用了 mod_expires 和 mod_rewrite。在我检查了这两个之后,一切都恢复正常了。

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

sudo a2enmod expires
sudo a2enmod rewrite

然后重启apache

service apache2 restart

希望这对那里的人有所帮助!

【讨论】:

谢谢。 mod rewrite 还不够,我还得加上 expires。【参考方案3】:

如果您的重写规则仍然不起作用,请记住一件事:

同时激活 ModRewrite 模块!它在 Ubuntu 中不是默认设置。

请参阅 other answer here 了解如何执行此操作。

【讨论】:

这是我的问题。我启用了该模块,但我的 .htaccess 文件丢失了RewriteEngine on。这就是我从另一个配置中弄乱复制/粘贴所得到的! 你能把这个变成一个完整的答案吗?【参考方案4】:

在我的情况下,问题是 .htaccess 文件的权限。

解决方案:

sudo chown apache:apache .htaccess

【讨论】:

以上是关于Apache 服务器忽略 .htaccess的主要内容,如果未能解决你的问题,请参考以下文章

使用 .htaccess 时 Apache 服务器上出现 403 和 500 错误

在Apache中使用.htaccess文件

.htaccess 适用于 Apache 服务器,但不适用于 FTP 目录

.htaccess 适用于 Apache 服务器,但不适用于 FTP 目录

apache的.htaccess文件作用和相关配置

如何在 Apache 服务器(和其他服务器)上检测 PHP 中对 .htaccess 的支持