2012-09-27 50 views
0

我想使用mod_rewrite只保留url中的查询字符串,并删除index.php?p =。.htaccess在URL中重写查询字符串

例如,这个链接

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

进入

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

所有网页都通过控制脚本的index.php和查询P =名字载入,这样

<?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

试试这个:

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

但要注意,如果您有多个的情况下(例如一些其他类型的URL重写的),要小心的RewriteRules的排序,因为你可以很容易地覆盖一个与另一个..

+0

有一个小的修改,它的工作原理:RewriteRule ^(。*)。html $ index.php?p = $ 1 [L] –

+0

我很抱歉,我刚从我上一个项目.htaccess文件复制了一行。并没有修改它,所以我现在编辑我的答案:) – Develoger