.htaccess在url中重写查询字符串

时间:2012-09-27 09:16:27

标签: apache .htaccess

我正在尝试使用mod_rewrite只将查询字符串保留在url中,并删除index.php?p =。

例如,此链接

http://domain.com/index.php?p=page-name-with-dashes

http://domain.com/page-name-with-dashes.html

所有页面都通过控制脚本index.php和查询p = name加载,如下所示

<?php
$page = isset($_GET['p']) ? $_GET['p'] : 'home';
...
require_once 'content-' . $page . '.php';
...
?>

我已经尝试了下面的所有变体,但都没有效果 - 有些给404有些给出了500个错误。

#RewriteRule ^index.php?p=(.*) $1/

#RewriteRule ^([^/]+)/?$ /index.php?p=$1 [QSA,L]

#RewriteRule ^(.*)$ index.php?p=$1 [NC,L,QSA]

我做错了什么?我对htaccess规则了解不多,所以请原谅我的新手问题。

1 个答案:

答案 0 :(得分:1)

试试这个:

RewriteRule ^(.*).html$  index.php?p=$1 [L]

但要注意,如果你有多个案例(例如其他类型的url要重写),请注意RewriteRules的排序,因为你可以轻松地用另一个重写...