如何在Ubuntu和Debian中启用Apache mod_rewrite模块

时间:2019-05-19 01:25:56  来源:igfitidea点击:

Apache mod_rewrite模块是一个基于已定义规则的重写引擎。
Apache重写引擎将URL映射到目录路径以及其他URL。
在本教程中,我们将学习如何启用Apache mod_rewrite模块,并配置VirtualHost使用文档根目录下可用的.htaccess文件。
当我们在配置文件中使用正确的重写规则时,有时你会遇到Apache服务器没有读取你的。htaccess或Apache没有重写url的问题。
这是因为没有启用Apache重写模块。
当我们安装了一个新的Apache服务器,mod_rewrite默认是不启用的,所以要使用rewrite配置,你需要手动启用mode_rewrite模块。

启用Apache2 mod_rewrite模块

我们使用a2enmod命令来启用Apache2 web服务器中的任何模块。
因此,使用以下命令在Apache设置中启用 mod_rewrite模块。

sudo a2enmod rewrite

为虚拟主机启用.htaccess

启用Apache rewrite模块后,现在需要在虚拟主机配置文件中添加“ AllowOverride All

<VirtualHost *:80>
    ServerName  www.example.com
	DocumentRoot /var/www/html
	
	<Directory /var/www/html>
		AllowOverride All
	</Directory>
	
</VirtualHost>

还可以通过编辑Apache主配置文件来全局启用该设置。

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

请输入

在Apache中启用mod_rewrite模块之后,还需要重新加载Apache2服务器,以将所有配置重新加载到运行的环境中。

sudo systemctl restart apache2