通过.htaccess更改URL结构


Changing URL structure through .htaccess

我知道apache.htaccess文件能够重写URL,使其更可读、更高效,但我不知道如何实现这一点。

假设我有以下几种URL:

http://mywebsite.com/index.php?mode=home
http://mywebsite.com/index.php?mode=articles
http://mywebsite.com/index.php?mode=about

但我希望这些看起来更好,可读性强。类似这样的东西:

http://mywebsite.com/home
http://mywebsite.com/articles
http://mywebsite.com/about

对于像我这样的apache新手来说,这似乎是一项复杂的任务。

如果有任何帮助,我将不胜感激。

将此代码放在DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)/?$ index.php?mode=$1 [QSA,L]

参考:Apache mod_rewrite简介