How to Write a PHP Query String Without Question Mark - A Step by Step Tutorial

How to Write a PHP Query String Without Question Mark - A Step by Step Tutorial
Page content

Why Should You Use A PHP Query String Without a Question Mark?

Today’s websites are dynamic and use databases and scripting languages. Therefore lots of queries are passed to the server to return to return some result. When scripting languages like ASP, PHP, ColdFusion, etc. pass these requests — or in technical terms, queries — they are usually passed after a ‘?’ question mark.

A simple search using Google for the word ’news’ returns a url like this:

https://www.google.co.in/search?hl=en&client=firefox-a&hs=nsW&rlz=1R1GGGL_en___IN396&q=news&aq=f&aqi=g8g-z1g1&aql=&oq=&gs_rfai=

Notice that there is a ? after the word search and it is followed by some information that the server understands. Our search term for the word news come a little further down the query, towards the middle where it reads, &q=news. Instead of such a long string of query, wouldn’t it have been nice to have something like www.google.com/search/news

Not only is the url human-understandable, it is also very search engine friendly, if your site is live and it wants to climb up the ladder to the top rank. With such an easy url, you can easily understand that a search has been done for the word ’news.’ Although not every website can use this form of query string without question mark because at times they might need other information, you can learn how to create it for simple website that you might design in the future.

Image Credit: Wikipedia

How to Write A Query String in PHP without a Question Mark

If you are into developing applications in PHP, writing a PHP query string without a question mark is really easy. With a few modifications, you should be able to successfully write

PHP Query String Without Question Mark

nice query strings that are readable and makes sense to both humans and search engines. To achieve this in PHP, a few modifications have to made to the .htaccess file in the root directory that you have placed on your website server. You can use any free PHP editor or IDE to write your PHP code and test the results.

To understand the process of writing a PHP query string without question mark, let us consider that you own a domain called www.example.com. Let the home page contain two text boxes; one for searching the name and one for searching the location. When the submit button is clicked after the appropriate data is entered, the URL returned will look something like

https://www.example.com/search?name=John&location=california

To access the query that comes after the question mark ‘?’ you have to use the $_SERVER[‘QUERY_STRING’] global array that holds the data that comes after the question mark. To check the data that comes after the question mark, use the echo function that is provided by PHP to display data on the web page.

The echo $_SERVER[‘QUERY_STRING’] will display name=John&location=california

Our requirement is to have the url look like

https://www.example.com/John/california

To achieve this result create a .htaccess file in the same directory where the page is present (in our case, it is the index.php page that is loaded by default.) In the .htaccess file write the following two lines.

RewriteEngine on

RewriteRule .* index.php

Save and close this file. Refresh or reload the default web page where you enter the query. Write any query and submit the form to see the results. You will see that the query will not have any question marks and you will get the url as desired that is search engine friendly and makes sense to any user.