choose.php - ChooSE your search engine

Andreas Schamanek

2020-12-12 20:41


choose.php is a PHP script intended to let users choose their favorite search engine. Instead of providing a link to a particular web search one can point to an installation of choose.php which allows users to choose a preferred search engine before doing the actual search.

The script is meant to be used for instance in blog posts and comments1 or in electronic mail communication (for this purpose the script produces a link to itself to be copied into the message).

The basic idea is that people should have a free and easy to use choice of search engines.

ChooSE @wox.at

Features

Instructions for use

Feel free to use my own installation of choose.php at https://wox.at/s/. However, I recommend that people set up their own search engine switcher (for added freedom of choice and privacy).

Basically, choose.php comes as a single PHP file. Download it (see links below), rename appropriately, set permissions if needed, and edit the file to change a few settings. That's it.

Important settings to make it work are values of $me, $mesuffix, and $c_action. Let's assume you have a blog at http://blog.rats.at. You copied choose.php to the main directory, so it should be available at http://blog.rats.at/choose.php. Then set

$me='http://blog.rats.at/choose.php';
$mesuffix='?q=';
$c_action="$me";

URL rewriting

For https://wox.at/s, I use URL rewriting (on an Apache server) to remove the filename from the URL. I copied choose.php as index.php into the folder /s. My .htaccess contains

RewriteEngine   on
RewriteBase /s

RewriteRule ^$                        index.php  [L]

RewriteCond %{REQUEST_FILENAME}       !-f
RewriteCond %{REQUEST_FILENAME}       !-d
RewriteRule (.*)                      index.php?q=$1  [QSA,L]

With these rewriting rules I can set in index.php (i.e. choose.php)

$me='https://wox.at/s';
$mesuffix='';
$c_action="$me/";

Dealing with slashes

If you use the rewriting rules as shown above you'll probably observe that when searching for a string with 2 or more slashes (//) the Apache server will reduce the slashes to single slashes3.

A solution is to use RewriteCond %{THE_REQUEST} and %1 as in

RewriteEngine   on
RewriteBase /s

RewriteRule ^$                        index.php  [L]

RewriteCond %{REQUEST_FILENAME}       !-f
RewriteCond %{REQUEST_FILENAME}       !-d
RewriteCond %{THE_REQUEST}            ^GET\ /s/([^\s]+)
RewriteRule .                         index.php?q=%1  [QSA,L]

Speaking of slashes, in rare cases they are encoded as %2F instead of %252F. Apache by default does not accept %2F and returns a 404. One could probably write a RewriteRule for this, but another approach is to turn AllowEncodedSlashes on. However, this needs to be done in the server configuration:

# Needed for ChooSE but should not hurt otherwise either AllowEncodedSlashes On

Processing ? and & parameters

Eventually, we also want to pass ?ext and possibly other parameters to the script. For this we need another rule which needs to be inserted right after RewriteRule ^$ index.php [L]:

RewriteCond %{REQUEST_FILENAME}       !-f
RewriteCond %{REQUEST_FILENAME}       !-d
RewriteCond %{THE_REQUEST}            ^GET\ /s/(.*)\?(.*)\ HTTP/
RewriteRule .                         index.php?q=%1&%2  [QSA,L]

Download & source


  1. Example usage in a comment: "Have you tried to look it up? Most search engines provide good results, see https://wox.at/s/choose.php?q=what+does+RTFM+mean"↩︎

  2. One user wanted to use my installation of choose.php as home page for the browser but wanted to be able to pre-select her own favorite search engine. Links for this setup as home page are provided as anchors behind the access keys as shown in the extended listing.↩︎

  3. Cf. Can mod_rewrite preserve a double slash? and How do they pass URLs through modrewrite?↩︎

  4. Version history is included in main file.↩︎

  5. Cf. MDN: OpenSearch description format↩︎