在Apache2中,您可以使用mod_rewrite模块来设置URL重写。以下是一些基本步骤和示例,以帮助您开始使用URL重写:
mod_rewrite模块。在终端中运行以下命令:sudo a2enmod rewrite
sudo systemctl restart apache2
/etc/apache2/sites-available/目录下),找到<Directory>部分,确保允许.htaccess文件进行覆盖。例如:<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
这里,AllowOverride All表示允许.htaccess文件中的所有重写规则。
在网站的根目录下创建或编辑.htaccess文件。例如,在/var/www/html/目录下创建一个名为.htaccess的文件。
在.htaccess文件中设置URL重写规则。以下是一些示例:
.php扩展名的URL重写为不带扩展名的URL:RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^?]*)$ index.php?path=$1 [L,QSA]
在这个例子中,访问http://example.com/some-page将被重写为http://example.com/index.php?path=some-page。
.htaccess文件并重启Apache2服务以使更改生效:sudo systemctl restart apache2
现在,您的Apache2服务器应该已经根据您在.htaccess文件中设置的规则进行URL重写。请根据您的需求调整示例中的规则。