37

我创建了一个仅包含以下行的 .htaccess 文件:

选项 - 索引

但是,仍会为目录显示索引。

我刚刚安装了 Apache2 并使用了所有默认值(我没有修改 apache2.conf 或 httpd.conf)。

操作系统:Ubuntu 12.04 (Precise Pangolin)
Apache2 版本:Server version: Apache/2.2.22 (Ubuntu) Server built: Feb 13 2012 01:51:56

$ ls -l .htaccess
-rwxr-xr-x .htaccess

编辑:

我接受了 lanzz 的建议,在.htaccess文件中添加了乱码,发现.htaccess文件没有被读取。

4

8 回答 8

54

您应该检查 Apache 配置是否允许执行 .htaccess。在您的虚拟主机配置中,应该有一行:

AllowOverride All

如果没有,这就是 .htaccess 没有生效的原因。

于 2012-05-26T21:22:54.100 回答
30

为了使其正常工作,我将以下内容添加到/etc/apache2/httpd.conf(安装 Apache 时默认为零长度文件),然后重新启动 Apache。现在Options -Indexes.htaccess文件中按需要工作。这是让它工作的最低要求:

/etc/apache2/httpd.conf :

<VirtualHost *:80>
        DocumentRoot /var/www
        <Directory / >
        </Directory>
</VirtualHost>

lanzz建议在文件中添加一行乱码以.htaccess查看它是否正在被读取,这有助于诊断问题。

请注意,根据Evan MulawskiAllowOveride的评论,默认为,因此在上面的最小行集中不需要它。Allhttpd.conf

于 2012-05-26T23:07:56.363 回答
16

I had the same problem. I was using a virtual host so I modified httpd-vhosts.conf so if you are using one this could help

Where you configure your host reference the directory tag to be the same as your document root

<VirtualHost *:8080>
ServerName somelocalserver
DocumentRoot "C:/Websites/htdocs/yoursite"
<Directory "C:/Websites/htdocs/yoursite">
    Options FollowSymLinks
    AllowOverride All
</Directory>

I needed AllowOverride All to recognize the .htaccess file.

Edit

I also needed to add the path to the site root in the directory tag itself. i.e. <Directory "C:/Websites/htdocs/yoursite">. This was the default in the samples I used.

于 2014-03-28T02:33:31.083 回答
9

As there's no httpd.conf file anymore, what you need to do on a VPS is:

  1. Go to /etc/apache2/sites-enabled/
  2. Do ls to find the file for the website you're looking for
  3. Edit the corresponding file with nano THE_CONF_FILE or with whatever editor you'd like
  4. Change all AllowOverride None values that you see in different <Directory> ... </Directory>s to AllowOverride All
  5. Save the file and close it

  6. Now you need to enable module rewrite by running the command:sudo a2enmod rewrite

  7. And finally to activate the new configuration, you need to run: service apache2 restart

It'll work like a charm now!

于 2014-11-25T19:24:12.447 回答
5

如果你有

AllowOverride None

将其更改为:

AllowOverride All

在您的 httpd.conf 或 /etc/apache2/sites-available/...conf 中的默认虚拟主机文件中

于 2013-12-17T16:56:35.977 回答
3

I solved this problem on RHEL by editing file /etc/http/conf/httpd.conf I changed all

AllowOverride None

To

AllowOverride All

Also you can check httpd.conf AllowOverride by following command on RHEL

grep -i AllowOverride /etc/conf/conf/httpd.conf
于 2016-08-10T19:11:51.620 回答
1

Also, beside changing AllowOverride All to the vhost configuration file, you may also need to add the following at the beginning of the .htaccess file:

RewriteEngine On
RewriteBase /

Otherwise may still not detecting the rewrites or redirects.

于 2016-01-06T18:22:16.237 回答
0

One more reason one's htaccess file is not being read is an existing htaccess enforces a HTTPS 301 redirect that the browser then caches which can be a problem if virtual hosts are not configured correctly. Full explanation below.

If one is setting up a virtual host in Apache and XAMPP for example, the first time you fire up the URL of your virtual host, if you have an existing htaccess file with a HTTPS 301 redirect, your browser will get redirected to the HTTPS version of your virtual host. But there's a potential problem. If you haven't explicitly set up a *:433 virtual host for the domain in question, in your Apache vhosts config file, Apache will default to using the first virtual host listed.

Depending on the contents of this default virtual host it may be impossible to distinguish what is happening, and user will maddeningly try to determine why changes to the htaccess file are no longer being processed.

Once you determine what is happening, the workaround is to disable any HTTPS redirects and reboot and try again without HTTPS. The real solution is to complete a full virtual host 443 setup with certificate.

于 2019-12-29T11:46:21.857 回答