Regex .htaccess重写规则无法按我需要的方式运行

Regex .htaccess重写规则无法按我需要的方式运行,regex,apache,.htaccess,mod-rewrite,Regex,Apache,.htaccess,Mod Rewrite,我对.htaccess和rewriter规则有一些问题。我已经搜索了几个小时类似的主题,但似乎没有任何效果。你知道我的代码有什么问题吗 所以我想让你http://localhost/myproject/post.php?p_id=92 像这样http://localhost/myproject/post/92. 如果我使用下面的代码,页面http://localhost/myproject/post/92 加载,但没有帖子内容(只有边栏、导航等) 在.htaccess中,我有: RewriteE

我对.htaccess和rewriter规则有一些问题。我已经搜索了几个小时类似的主题,但似乎没有任何效果。你知道我的代码有什么问题吗

所以我想让你http://localhost/myproject/post.php?p_id=92 像这样http://localhost/myproject/post/92. 如果我使用下面的代码,页面http://localhost/myproject/post/92 加载,但没有帖子内容(只有边栏、导航等)

在.htaccess中,我有:

RewriteEngine on
RewriteRule ^(index|contact|registration)$ $1.php [NC,L]
RewriteRule ^post/(\d+)$ post.php?p_id=$1 [NC,L]
在HTML中,我有:

<a href="post/<?php echo $post_id; ?>"><?php echo $post_title ?></a>
或者我可以创建URLhttp://localhost/myproject/92 更改如下:

RewriteRule ^(\d+)$ post.php?p_id=$1 [NC,L] 
如何使URL看起来像http://localhost/myproject/post/92 ? 我不明白为什么上面的两个选项有效,但我需要的一个不起作用

提前谢谢

这样做:

Options -MultiViews
RewriteEngine on

RewriteRule ^post/(\d+)/?$ post.php?p_id=$1 [NC,L,QSA]

RewriteRule ^(index|contact|registration)/?$ $1.php [NC,L]
两项改变是:

  • 关闭
    多视图
    ,以便关闭内容协商服务
  • post
    规则保持在顶部,这样当您在该规则的替代项中添加
    post
    时,其他人可能不会影响它
  • Options -MultiViews
    RewriteEngine on
    
    RewriteRule ^post/(\d+)/?$ post.php?p_id=$1 [NC,L,QSA]
    
    RewriteRule ^(index|contact|registration)/?$ $1.php [NC,L]