2012-01-06 143 views
3

我在.htaccess中的重写规则遇到了一些问题,这里没有任何其他的答案似乎有窍门!.htaccess重写规则不起作用

基本上下面的代码工作

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,但我似乎已经在第一个障碍中倒下了!

任何想法的人?任何帮助将受到感谢!

+0

对于非显而易见的问题有时需要来设置[RewriteLog](http://httpd.apache.org/docs/2.0/mod/mod_rewrite的.html#rewritelog)。 – mario 2012-01-06 19:08:52

回答

4

见下

#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] 

以下编辑代码如果这个规则

重写规则^ 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] 

编辑

我想借project.php?ID = 82,并切换到项目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] 
+0

第二部分工作,让它重定向到test.php! – 5ummer5 2012-01-06 19:22:36

+0

我加了([0-9] +)代替82,而且效果也很好。你知道我会怎么把它重定向到project-82.html或者ID是什么吗?谢谢!! – 5ummer5 2012-01-06 19:23:50

+0

@ user1134974我不明白这个问题:你能给我一个例子 – 2012-01-06 19:27:22

0

见规则自你说index.html改写工作正常我猜你已经有这个,但不是假设我仍然建议你确保rewri tes实际上已启用。我总是在我的.htaccess文件把这个第一:

RewriteEngine On