user5307298 user5307298 - 6 months ago 42
PHP Question

htaccess rule for search.php page

I am looking for correct htaccess rules for my search.php page. Because I tried several, but nothing worked. It may be possible my existing htaccess rules conflicting it. So I am also pasting my current htaccess file here. Currently I have following search page url with query string:

http://my-domain.com/search.php?q=keyword


I want the following clean url:

http://my-domain.com/search/keyword


My HTML form is:

<form method="get" action="search.php">
<input type="text" name="q" class="txtfield" value="<?php echo $q; ?>" placeholder="Search post here..." />
</form>


htaccess:

Options +FollowSymlinks -MultiViews

RewriteBase /

#Enable mod rewrite
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ page.php?category=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/]+)$ page.php?category=$1&post=$2 [QSA,L]


Note that I don't have search related htaccess rule in above file because as I said nothing worked. I always saw 404 page. That's why I removed it.

Also If I put submit button in the form in some future like below:

<form method="get" action="search.php">
<input type="text" name="q" class="txtfield" value="<?php echo $q; ?>" placeholder="Search post here..." />
<input type="submit" name="btnsearch" class="btn" value="Search" />
</form>


My search url will change to:

http://my-domain.com/search.php?q=keyword&btnsearch=Search


Then I am wondering what correction/modification I will have to do in the correct htaccess rule which you will give to me as per my first query? Should we have two rules if the search url contains two or more than two query strings? Please help me on this guys. Thanks.

Answer

When a form is submitted it will indeed make your URL as:

http://my-domain.com/search.php?q=keyword&btnsearch=Search

To keep submitted URL pretty you need to prevent form submission using Javascript code and make URL pretty like this.

<html><head>
<script>
function prettySubmit(form, evt) {
   evt.preventDefault();
   window.location = form.action + '/' + form.q.value.replace(/ /g, '+');
   return false;
}    
</script>
</head><body>
<form method="get" action="search" onsubmit='return prettySubmit(this, event);'>
   <input type="text" name="q" value="<?php if (isset($_GET['q'])) echo $_GET['q']; ?>"
       class="txtfield" placeholder="Search post here..." />
   <input type="submit" name="btnsearch" class="btn" value="Search" />
</form>
</body></html>

This will submit your form to a pretty URL:

http://my-domain.com/search/keyword

Now you will need this rule in your root .htaccess to handle this pretty URL;

Options -MultiViews
RewriteEngine On

RewriteRule ^search/(.+)$ search.php?q=$1 [L,QSA,NC]

Update:

Here is a way to do it completely using mod_rewrite rules without using any JS. Use following rules in root .htaccess:

Options -MultiViews
RewriteEngine On

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

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