我在.htaccess中的Rewrite规则时遇到了一些麻烦,这里的其他答案似乎都无法解决问题!
基本上下面的代码有效
RewriteRule ^index.php$test.php
但是,只要添加其他内容,它就不会!下面的两行代码根本不起作用
RewriteRule ^project-([0-9]+)\.html$project.php?id=$1
RewriteRule ^project.php?id=82$test.php
当我开始工作时,我最终希望从页面中获取项目标题并将其插入url(就像您在URL中查看该站点一样)并将所有.php都转换为.html,但是我似乎拥有跌倒了第一关!
有任何想法吗?任何帮助将不胜感激!
解决方法:
请在下面查看对您的代码的以下修改
#escape the . so it does not match indexsphp etc
#added [NC,L] so it is not case sensitive and last rule processed
RewriteRule ^index\.php$test.php [NC,L]
#added [NC,L] so it is not case sensitive and last rule processed
# and QSA to pass through any existing query string values
RewriteRule ^project-([0-9]+)\.html$project.php?id=$1 [L,NC,QSA]
如果这条规则
RewriteRule ^project.php?id=82$test.php
仅当id = 82时才匹配,那么它将不起作用,需要按以下方式重写
#if the query string has an id=82
RewriteCond %{QUERY_STRING} id=82 [NC]
#and resource is project.php, send it to test.php
RewriteRule ^project\.php$test.php [NC,L]
编辑
I would like to take project.php?id=82 and change to project-82.html
请参阅以下规则
#if the original request has project.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project\.php [NC]
#capture the id param
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
#and 301 redirect to project-id.html
RewriteRule . project-%1.html? [L,R=301]