.htaccess不工作的Apache

我有一台服务器从亚马逊的EC2服务运行在Linux Ubuntu的,我已经安装了Apache,PHP和MySQL。 我在文档根目录/var/www/html添加了一个.htaccess文件。 我在其中input了这个代码: ErrorDocument 404 /var/www/html/404.php ,它仍然没有显示出来。 我不断input此命令多次: sudo service httpd restart启动服务器,但没有显示更改显示…我怎样才能解决这个问题…我做错了什么? 提前致谢!

首先,请注意,重新启动httpd对于.htaccess文件不是必需的。 .htaccess文件专门针对没有root权限的用户 – 即无法访问httpd服务器configuration文件,无法重新启动服务器。 由于您可以重新启动服务器,因此不需要.htaccess文件,可以直接使用主服务器configuration。

其次,如果.htaccess文件被忽略,你需要检查AllowOverride设置是否正确。 有关详细信息,请参阅http://httpd.apache.org/docs/2.4/mod/core.html#allowoverride 。 您还需要确保将其设置在正确的范围内 – 即在您的configuration的右侧块中。 例如,确保你没有编辑块中的那个。

第三,如果你想确保一个.htaccess文件实际上被读取,就把垃圾放进去。 当您将浏览器指向包含该文件的目录时,您的.htaccess文件中的无效行(例如“INVALID LINE HERE”)将导致500服务器错误。 如果没有,那么你没有正确configurationAllowOverride。

  1. configurationApache mod_rewrite

    a2enmod rewrite

  2. 将以下代码添加到/etc/apache2/sites-available/default

    AllowOverride All

  3. 重新启动Apache

    /etc/init.d/apache2 restart

如果你已经尝试了上述所有的方法,哪一个都是有效的,而且你的htaccess文件不能正常工作,或者正在读取,请在apache2.conf文件中修改指令。 在Ubuntu下,path是/etc/apache2/apache2.conf

更改指向您的公共网页,其中htaccess文件驻留的<Directory>指令。 从AllowOverride None更改为AllowOverride All

 <Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> 

我有同样的问题,并find了关于Ubuntu的问题的答案和解释! 论坛https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working

对于Ubuntu,
首先,运行这个命令:

 sudo a2enmod rewrite 

然后,使用以下命令使用nano或vim编辑文件/etc/apache2/sites-available/000-default.conf : –

 sudo nano /etc/apache2/sites-available/000-default.conf 

然后在000-default.conf文件中,将此行添加到DocumentRoot /var/www/html 。 如果你的根html目录是其他的东西,那就写下:

 <Directory "/var/www/html"> AllowOverride All </Directory> 

做完所有事情后,使用命令sudo service apache2 restart启动apache

最有可能的是,AllowOverride被设置为None。 位于/ etc / apache2文件夹中的apache2.conf的Directory部分

尝试将其设置为AllowOverride All

WampServer打开WampServer托盘图标—-> Apache —> Apache模块—> rewrite_module

使用RewriteBase / {你的文件夹} /你的.htaccess

只要按照3个步骤

  1. 使用以下命令启用mode_rewrite

    sudo a2enmod重写

密码将被询问。 所以input你的密码

  1. 更新位于/ etc / apache2 / sites-available /目录中的000-default.confdefault.conf文件。 你不能直接编辑它。 所以用下面的命令打开

    sudo gedit /etc/apache2/sites-available/000-default.conf

或者sudo gedit /etc/apache2/sites-available/default.conf

你会得到

DocumentRoot / var / www / html

要么

DocumentRoot / var / www

线。 之后添加下面的代码。

 <Directory /var/www/html/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> 

使用户的目录标签path与文件中显示的相同。

  1. 使用以下命令重新启动您的apache服务器

    sudo服务apache2重启