2012-02-16 71 views
-1

我需要重寫URL像http://www.example.com/search.php?abchttp://www.example.com/abc 使用下面的規則,但它不起作用,爲什麼它不匹配?HTTP的RewriteCond重寫規則和

 
RewriteCond %{REQUEST_URI} ^search.php 
RewriteRule ^search.php?q=([-0-9a-zA-Z]+) $1 

回答

1
  • %{REQUEST_URI}總是有/在開始。所以RewriteCond %{REQUEST_URI} ^search.php永遠不會匹配。

  • RewriteRule Directive

    Pattern是一個perl兼容的正則表達式。在第一個RewriteRule上,它被應用到請求的(%-decoded)URL路徑;隨後的模式應用於最後匹配的RewriteRule的輸出。

  • ?表示匹配前一個字符或沒有逃脫時的前一個字符。所以,你的^search.php?q=...將匹配:

    • search.phpq=...
    • search.phq=...
PCRE man pages

?擴展的( 也爲0或1量詞 也量詞極小

的意思你將不得不這樣做:

Options +FollowSymLinks 
RewriteEngine On 

RewriteCond %{ENV:REDIRECT_STATUS} 200 
RewriteRule^- [L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteCond %{REQUEST_URI} ^/([\w]+)/?$ [NC] 
RewriteRule^search.php?q=%1 [L,QSA] 

RewriteCond %{REQUEST_URI} ^/search.php [NC] 
RewriteCond %{QUERY_STRING} ^q=([\w\d-]+)$ [NC] 
RewriteRule^/%1? [L,R=301] 

+0

什麼是NC和L? – user121196 2012-02-16 11:14:11

+0

@ user121196檢查答案中的更新。 – ThinkingMonkey 2012-02-16 11:17:43

+0

不,它仍然不起作用 – user121196 2012-02-16 11:18:33