.htaccess 未在 var/www 中读取,但在 /var 中读取

.htaccess 未在 var/www 中读取,但在 /var 中读取

我使用 apt-get install 安装了 apache2.2、php5 和 mysql。我使用 vmware 来安装 Ubuntu 11.04 VM。我无法在文档根目录中读取 .htaccess。这是我的 apache.conf:

#
# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
#
LockFile ${APACHE_LOCK_DIR}/accept.lock

#
# PidFile: The file in which the server should record its process
# identification number when it starts.
# This needs to be set in /etc/apache2/envvars
#
PidFile ${APACHE_PID_FILE}

#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 300

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 15

##
## Server-Pool Size Regulation (MPM specific)
## 

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

# worker MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadLimit: ThreadsPerChild can be changed to this maximum value during a
#              graceful restart. ThreadLimit can only be changed by stopping
#              and starting Apache.
# ThreadsPerChild: constant number of worker threads in each server process
# MaxClients: maximum number of simultaneous client connections
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_worker_module>
    StartServers          2
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

# event MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxClients: maximum number of simultaneous client connections
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_event_module>
    StartServers          2
    MinSpareThreads      25
    MaxSpareThreads      75 
    ThreadLimit          64
    ThreadsPerChild      25
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

#
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives.  See also the AllowOverride
# directive.
#

AccessFileName .htaccess

#
# The following lines prevent .htaccess and .htpasswd files from being 
# viewed by Web clients. 
#
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>

#
# DefaultType is the default MIME type the server will use for a document
# if it cannot otherwise determine one, such as from filename extensions.
# If your server contains mostly text or HTML documents, "text/plain" is
# a good value.  If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# keep browsers from trying to display binary files as though they are
# text.
#
DefaultType text/plain


#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off

# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog ${APACHE_LOG_DIR}/error.log

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn

# Include module configuration:
Include mods-enabled/*.load
Include mods-enabled/*.conf

# Include all the user configurations:
Include httpd.conf

# Include ports listing
Include ports.conf

#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
# If you are behind a reverse proxy, you might want to change %h into %{X-Forwarded-For}i
#
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\""     vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

# Include of directories ignores editors' and dpkg's backup files,
# see README.Debian for details.

# Include generic snippets of statements
Include conf.d/

# Include the virtual host configurations:
Include sites-enabled/

这是 httpd.conf,请注意 DocumentRoto 和 Directory 指令:

DocumentRoot /var/www
<Directory /var>
AllowOverride All
</Directory>
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so 
RewriteLog /var/rewrite.log
RewriteLogLevel 9

这是位于 /var 中的 .htaccess 文件,因为如果我将其放在 /var/www 中,它就不会被读取:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?((?:.+?)[^/])$ /$1/ [L,R=301]

我希望此规则在 URL 末尾添加一个 /(如果 URL 尚未存在)。它有点奏效……如果我转到,它localhsot/t会带我到localhost/www/t/;如果我删除/它,它会带我到,localhost/wwww/www/t/依此类推。它不断在末尾添加一个/www和一个/。我认为这与 .htaccess 文件的位置问题有关。我相信它应该在我的文档根目录 (/var/www) 中,但即使我在 httpd.conf 中指定了它,apache 也不会从这里读取它。

有什么想法吗?更多背景信息以及与一位非常乐于助人的开发人员的长时间交流: https://stackoverflow.com/questions/11586074/making-rewriterule-to-mock-reddits-or-rails/11586401#comment15335191_11586401

编辑:这是 mod_rewrite 日志(调试设置为 9):

127.0.0.1 - - [24/Jul/2012:19:11:46 --0500] [localhost/sid#213e6480][rid#2159b028    /initial] (3) [perdir /var/] add path info postfix: /var/www/www -> /var/www/www/r/all/
127.0.0.1 - - [24/Jul/2012:19:11:46 --0500] [localhost/sid#213e6480][rid#2159b028/initial] (3) [perdir /var/] strip per-dir prefix: /var/www/www/r/all/ -> www/www/r/all/
127.0.0.1 - - [24/Jul/2012:19:11:46 --0500] [localhost/sid#213e6480][rid#2159b028/initial] (3) [perdir /var/] applying pattern '^/?((?:.+?)[^/])$' to uri 'www/www/r/all/'
127.0.0.1 - - [24/Jul/2012:19:11:46 --0500] [localhost/sid#213e6480][rid#2159b028/initial] (1) [perdir /var/] pass through /var/www/www

答案1

您提到使用apt-get安装所有这些,但您没有提到使用 禁用 Debian 软件包提供的默认站点a2dissite default,这意味着默认设置仍从 /etc/apache2/sites-enabled/default 加载。该默认站点的一部分是:

    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>

这将覆盖您的设置。

要么删除您的配置并编辑默认站点配置以使用它,要么禁用默认站点并更新您的配置以包含其缺少的任何内容,然后您的.htaccess文件应该开始工作。

答案2

一个可能有帮助也可能没用的提示:

清除并关闭浏览器中的缓存。我曾经追踪过 htaccess 的问题,最后发现我的浏览器缓存了一个页面。在我看来,htaccess 似乎无法正常工作或偶尔工作,直到我发现了这一点。

可能无法解决您的问题,但至少是一个有用的信息。

相关内容