URL Rewriting For Cleaner Search Results using PHP and .htaccess

What exactly do we mean by URL Rewriting for better Search Results? Take the following case study as an example:

Say for example that you have a website running php and mysql that serves dynamic web pages, such as a search engine where a given search result is subsequently posted to the page. Every search will produce it’s own result page and these pages only exist as results but not as an actual hard coded html or php page for example. With a dynamic website, you can have one single (in the most simplistic terms) index.php file which contains all of the code to produce this functionality. The main benefit is that it’s a very lightweight approach. This is opposed so say, this actual website where every post that we publish is its own page or file living somewhere in the WordPress wp-content directory.

So let’s get to the point. Using our fictional search engine as a dynamic web page example, the resulting page’s URL will likely look something like this as the result of our query against the database.

https://allnightburger.com/search.php?q=keyword

Pretty boring and so very 1997, right? What you would likely want is something a little cleaner, pretty and more descriptive. Maybe you would want your results to look more like this for example:

https://allnightburger.com/results/keyword

Looks great and once you know how to do that, anything is possible. Yes. Anything.
So where do we start? The .htaccess file. That’s where.

1.PHP

The PHP portion of the url rewrite is done in two steps.
1. Note that the query portion of the GET has a header ‘Location’ with the word ‘results’. This can be anything you want and the word ‘results’ will be rewritten in the url in the next section.

2. In the  PHP code below we have put together a basic search form. Here we define “findme” in our GET method. This ‘findme’ is the word that the user is going to input in the search form and will be picked up by the htaccess RewriteRule.

<?php
if ($_GET[‘query’] != ”) {
header( ‘Location: /results/’ . $_GET[‘query’]);
}
?>
<br>
<br>
<form class=”example” action=”/” style=”margin:auto;max-width:350px” method=”get”>
<input type=”text” pattern=”[A-Za-z]*” oninvalid=”setCustomValidity(‘Please enter a word ‘)”
onchange=”try{setCustomValidity(”)}catch(e){}” placeholder=”Search..” name=”query” value=”<?php echo $_GET[“findme”]; ?>”>
<button type=”submit”><i class=”fa fa-search”></i></button>
</form>

2. htaccess

If you don’t have access to the command line, you may not be able to test it all out but if you do, this is a good place to start. To clean up the url, you can use the following RewriteRule with the included regex, will result in https://yourwebsite.com/results/findme, where ‘findme’ is the search term that was inputted in the form.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^results/([^/\.]+)$ /?findme=$1

And that is pretty much it. I have not tested this locally or with subdomains so Ideally you’ll have a test environment that includes an actual domain name.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.