2012-01-06 249 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