mod_rewrite,/ index.php?p = test到/ test

时间:2010-10-10 20:57:24

标签: mod-rewrite

我正在尝试在.htaccess文件中设置一个非常简单的规则,以便重写这种网址:www.domain.com/index.php?page = test到:www.domain.com/test

我是mod_rewrite的新手,到目前为止,我来到了这个规则

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RewriteRule ^(.*)$ index.php?$1

但它不起作用。

有任何帮助吗? 非常感谢

2 个答案:

答案 0 :(得分:3)

几乎是对的,你忘记了page=

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1

现在请注意,此规则恰恰相反:它会将/test内部路径的请求内部重写为/index.php?page=test不会反之亦然。

答案 1 :(得分:0)

另一种方法是:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([a-zA-Z0-9-_]+)/?$ index.php?page=$1.php [NC,L]

这也可以确保您的网址只能包含字母,数字,短划线和下划线。

Altough,在旁注中,我建议你为每个页面分别设置文件,包括header.php和footer.php ......

相关问题