2014-01-06 43 views
0

我正在php其實我需要SEO搜索查詢腳本我已經寫在文件夾中調用newtheme(文件夾名稱)。當我嘗試做搜索引擎優化網址不工作時沒有發現執行搜索時顯示。請檢查我的代碼並糾正我在哪裏做錯了。搜索引擎友好的網址搜索查詢字符串在PHP中使用htaccess

在我的htaccess

RewriteCond %{ENV:REDIRECT_STATUS} !200 
RewriteCond %{QUERY_STRING} ^searchword=([^&]+)$ 
RewriteRule ^results.php$ /newtheme/%1/? [L,R=301] 

在我results.php

if($_GET['searchword']; 
{ 
$q= mysql_real_escape_string($_GET['searchword']); 
$sql=mysql_query("select * from table where keyword='$q'"); 
$row=mysql_fetch_array($sql); 
echo $row['title']; 
} 
?> 

htaccess文件裏放在我的文件夾newtheme。腳本文件夾localhost/newtheme/.htaccess

+1

你應該閱讀關於SQL注入和'mysql_ *'函數。 http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – sshow

+0

你需要提供更多的細節,如什麼是你的鏈接網址?什麼是.htaccess等的位置 – anubhava

+0

@anubhava搜索表單代碼

user3164590

回答

0

作爲絕對最小值,請在將它提供給MySQL查詢之前,將它們轉義出來。

$q = mysql_real_escape_string($_GET['searchword']); 

如果我正確理解您的實現,你想海基會重寫針對其位於newtheme目錄中的文件results.php查詢,嘗試這樣的事情

RewriteRule ^searchword/([\ A-Za-z0-9-_,]+)?$ /newtheme/results.php?searchword=$1 [NC,L] 
+0

感謝您的回覆,但它不起作用,sef url顯示類似results.php?searchword = rose。但我需要作爲http:// localhost/newtheme /搜索/上升 – user3164590

+0

在搜索表單字段名稱我用作searchword,動作頁面是results.php,一般它工作http://localhost/newtheme/results.php?searchword =上漲,我需要作爲http:// localhost/newtheme/search/rose – user3164590

0

你會在你的/newtheme/.htaccess文件中需要這2條規則:

RewriteEngine On 
RewriteBase /newtheme/ 

# external redirect from actual URL to pretty one 
RewriteCond %{THE_REQUEST} \s/+newtheme/results\.php\?searchword=([^\s&]+) [NC] 
RewriteRule^search/%1? [R=302,L] 

# internal forward from pretty URL to actual one 
RewriteRule ^search/([^/.]+)/?$ results.php?searchword=$1 [L,QSA,NC] 
+0

仍然無法正常工作我已經改變了,正如你所說的,瀏覽器網址顯示results.php?searchword =執行搜索時的內容。 – user3164590

+0

好的.htaccess以上的位置是什麼?您的瀏覽器中的完整網址不起作用? – anubhava

+0

它是'/newtheme/results.php?searchword = something'還是隻是'/results.php?searchword = something'? – anubhava

相關問題