簡體   English   中英

使用htaccess在php中搜索查詢字符串的搜索引擎友好url

[英]search engine friendly url for search query strings in php using htaccess

我在php工作,實際上我需要SEO網址作為我寫在名為newtheme (文件夾名稱)的文件夾中的搜索查詢腳本的URL。 當我嘗試執行SEO url時,找不到執行搜索時顯示的URL。 請檢查我的代碼並糾正我做錯了的地方。

在我的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

絕對最低限度,請在將查詢詞輸入MySQL查詢之前先對其進行轉義。

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

如果我正確理解了您的實現,則您正在嘗試SEF針對位於newtheme目錄中的results.php文件重寫查詢,請嘗試類似這樣的操作

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

您將在/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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM