Anas Zine Anas Zine - 1 year ago 89
PHP Question

Search results mod rewrite with request uri

I am trying to create a search form for my website with search engine friendly urls.

I have the following form code on every page of my website :

<form class="form" method="get" id="searchform" action="searchrd">


Here is also my htaccess file :`

RewriteCond %{REQUEST_URI} searchrd$

RewriteCond %{QUERY_STRING} ^query=([^&]+)$
RewriteRule ^([^&]+)$ /results/%1? [R=301,L]
RewriteRule ^/?results/([^&]+)?$ results.php?query=$1 [NC]

My problem is the following :

When I go to homepage for example and enter my search with {search-term1} it gets me to http://{search-term1}

No problem to this step

But if I try a search on results page with {search-term2} I am getting this url http://{search-term1}?query={search-term2}

My question is how can I have this url{search-term2} for the second search even if i do the search on results page. I already searched for hours and modified flags, rules and conditions but nothing gave me what I wanted.


Answer Source

Use absolute path in your form action:

<form class="form" method="get" id="searchform" action="/searchrd">

And refactor your rules like this:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^query=([^&]+)$ [NC]
RewriteRule ^searchrd/?$ /results/%1? [R=301,L,NC,NE]

RewriteRule ^/?results/([^&]+)?$ results.php?query=$1 [NC,L,QSA]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download