在 apache2 中启用 .htaccess

在 apache2 中启用 .htaccess

这可能与 Ubuntu 本身没有直接关系,但这里的目录树不同,我有点困惑。

我尝试运行一个网站,它加载正常,但它严重依赖于 URL 重写,而我没有得到它,因为我的安装中可能未启用 .htaccess 指令。我已关注以下文章Ubuntu.com在我的 apache2 上启用 .htaccess 文件,但我无法按照它操作,因为它告诉我要更改的文件在它告诉我去的文件夹中不存在。

这是最接近的文件名default- 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
    DocumentRoot /var/www

    # 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

我没有看到<Directory /var/www/>这里有任何障碍。

如何启用我的 .htaccess 文件?我需要每个项目都有它们。

答案1

添加到虚拟主机:

<VirtualHost *:80>
   AccessFileName .htaccess (.htaccess is the default filename)
  ...
   <Directory /var/www/>
      Options Indexes FollowSymLinks Includes
      AllowOverride All
      Order allow,deny
      Allow from all
    </Directory>
...
</VirtualHost>

然后重新启动apache:

service apache2 restart

要检查其是否有效,请将以下行添加到 .htaccess :

ErrorDocument 404 /error404.html

对于错误 404(未找到页面),您的浏览器应该打开自定义错误页面,在这种情况下error404.html

答案2

sudo -i
sudo nano /etc/apache2/apache2.conf

如果你的 ubuntu 80 端口登陆这里/var/www/html

(仔细看不是 /var/www/

添加此块:

<directory /var/www/html>
     AllowOverride All
</directory>

相关内容