本文运行环境为ubuntu下root用户
1.加载mod_rewrite模块
命令
a2enmod
程序将显示可供加载的模块,输入:
rewrite
apache服务器将会自动加载mod_rewrite模块
2.修改网站配置文件
命令
cd /etc/apache2/sites-enabled/000-default
将
<Directory /home/yoursiteDirectory> <div>Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all</div> <div></Directory>[
中的AllowOverride None修改为AllowOverride All
3.直接在上述<Directory>内增加代码
RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
即可实现链接的重写
亦可在.htaccess内使用下列代码
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
4.重新加载apache服务器配置
/etc/init.d/apache2 reload
或者
/etc/init.d/apache2 restart
即可成功进行链接重写
注:
在apache官方文档中建议尽量将在.htaccess能实现的功能方法放在<Directory></Directory>内
因为使用.htaccess会降低apache服务器的性能
不知为什么csdn排版总是怪怪的...