- slide 1 of 2
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.
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
- slide 2 of 2
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 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
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
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.
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.