如何解决.htaccess RewriteRule无法按我需要的方式工作
.htaccess和RewriteRule遇到一些问题。我已经搜索了几个小时的类似主题,但似乎没有任何效果。您知道我的代码有什么问题吗?
所以我试图使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>
我已经测试了许多其他方式,并且看来,如果我删除post /或将-替换为-,则页面将按预期加载。
例如,我可以使用以下代码将URL http:// localhost / myproject / post-92设置为正确,并且可以正常工作:
RewriteRule ^post-(\d+)$ post.php?p_id=$1 [NC,L]
或者可以通过以下更改使URL http:// localhost / myproject / 92:
RewriteRule ^(\d+)$ post.php?p_id=$1 [NC,L]
如何使URL看起来像http:// localhost / myproject / post / 92?我不明白上面2个选项为何起作用,但我需要的那个却不起作用。
谢谢!
解决方法
具有这种方式:
Options -MultiViews
RewriteEngine on
RewriteRule ^post/(\d+)/?$ post.php?p_id=$1 [NC,L,QSA]
RewriteRule ^(index|contact|registration)/?$ $1.php [NC,L]
两个更改是:
- 关闭
MultiViews
,以便关闭内容协商服务 - 在顶部保留
post
规则,以便在该规则的替换中添加post
时,其他规则可能不会对其造成影响。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。