0

I am working on php actually I need SEO urls for search queries script I have written in folder called as newtheme (folder name). When I try to do SEO urls not working not found showing when perform search. Please check my code and correct me where i did wrong.

in my htaccess

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

In my 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 file placed at my folder inside as newtheme. script folder localhost/newtheme/.htaccess

2 Answers2

0

As an absolute minimum, PLEASE escape the search word before you feed it into the MySQL query.

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

If I understand your implementation correctly, you're trying to SEF rewrite a query against your results.php file which is located in the newtheme directory, try something like this

RewriteRule ^searchword/([\ A-Za-z0-9-_,]+)?$ /newtheme/results.php?searchword=$1 [NC,L]
  • Thanks for your reply, but it is not working, sef url showing like results.php?searchword=rose. but i need as http://localhost/newtheme/search/rose – user3164590 Jan 07 '14 at 05:31
  • in search form field name i used as searchword, action page is results.php, general it is working http://localhost/newtheme/results.php?searchword=rose, i need as http://localhost/newtheme/search/rose – user3164590 Jan 07 '14 at 05:44
0

You will need these 2 rules in your /newtheme/.htaccess file:

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • still not working i have changed as you told, browser url showing results.php?searchword=something when perform search. – user3164590 Jan 07 '14 at 07:10
  • Ok what is location of above .htaccess? And what is your full URL in browser that didn't work? – anubhava Jan 07 '14 at 07:16
  • Is it `/newtheme/results.php?searchword=something` OR just `/results.php?searchword=something`? – anubhava Jan 07 '14 at 07:16
  • when i perform search url showing like this http://localhost/newtheme/results.php?searchword=Wax+flower – user3164590 Jan 07 '14 at 07:29
  • Then make sure above .htaccess is in `/newtheme/.htaccess` and before all other rules there. – anubhava Jan 07 '14 at 07:32
  • when i change like this rule RewriteCond %{THE_REQUEST} \s/+newtheme/results\.php\?searchword=([^\s&]+) [NC] working but template css and images not loading please suggest me – user3164590 Jan 07 '14 at 07:41
  • url rewrite is working but theme template css, images not loading please help. now url coming like this http://localhost/newtheme/search/Suffron – user3164590 Jan 07 '14 at 07:48
  • Make sure to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with `http://` or a slash `/`. – anubhava Jan 07 '14 at 08:22
  • Thank You Anubhava. It's working now. Once again thank You. Great. – user3164590 Jan 07 '14 at 16:10