(as)  [sysadmin] [blog]

User Tools

Site Tools


choose_php

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both sides next revision
choose_php [2017-04-09 15:08]
andreas http -> https where appropriate
choose_php [2019-03-15 18:17]
andreas [Features] fixed link/name for Startpage
Line 1: Line 1:
 +====== choose.php - ChooSE your search engine ======
 +
 +**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 comments
 +((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]]")) 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 =====
 +
 +  * [[https://wox.at/s/]]
 +
 +===== Features =====
 +
 +  * Freedom of choice ;-)
 +  * Extended listing of search engines: Shown with ''?ext'' added to URL ([[https://wox.at/s/choose+your+search+engine?ext|example showing extended list]])
 +  * Preselect a search engine: A default search engine is set up by the script itself. It can be overruled by adding ''?e='' + the key for the search engine ([[https://wox.at/s/choose+your+search+engine?e=startpage|example for Startpage]]).((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.))
 +  * Search engines can be easily added or changed by editing the script.
 +  * Access keys for search engines: E.g. Alt-Shift-g for Google, Alt-Shift-b for Bing, ...
 +  * Access keys for search string field (Alt-Shift-f) and Submit (Alt-Shift-s)
 +
 +===== 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
 +<code php>
 +$me='http://blog.rats.at/choose.php';
 +$mesuffix='?q=';
 +$c_action="$me";
 +</code>
 +
 +==== 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
 +
 +<code htaccess>
 +RewriteEngine   on
 +RewriteBase /s
 +
 +RewriteRule ^$                        index.php  [L]
 +
 +RewriteCond %{REQUEST_FILENAME}       !-f
 +RewriteCond %{REQUEST_FILENAME}       !-d
 +RewriteRule (.*)                      index.php?q=$1  [QSA,L]
 +</code>
 +
 +With these rewriting rules I can set in ''index.php'' (i.e. ''choose.php'')
 +
 +<code php>
 +$me='https://wox.at/s';
 +$mesuffix='';
 +$c_action="$me/";
 +</code>
 +
 +=== 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 slashes((Cf. [[https://stackoverflow.com/questions/889435/can-mod-rewrite-preserve-a-double-slash |Can mod_rewrite preserve a double slash?]] and [[https://stackoverflow.com/questions/831355/diggbar-modrewrite-how-do-they-pass-urls-through-modrewrite/869017#869017 |How do they pass URLs through modrewrite?]])).
 +
 +A solution is to use ''RewriteCond %{THE_REQUEST}'' and ''%1'' as in
 +
 +<code htaccess>
 +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]
 +</code>
 +
 +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 [[https://httpd.apache.org/docs/2.2/en/mod/core.html#allowencodedslashes|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]'':
 +
 +<code htaccess>
 +RewriteCond %{REQUEST_FILENAME}       !-f
 +RewriteCond %{REQUEST_FILENAME}       !-d
 +RewriteCond %{THE_REQUEST}            ^GET\ /s/(.*)\?(.*)\ HTTP/
 +RewriteRule .                         index.php?q=%1&%2  [QSA,L]
 +</code>
 +===== Download & source =====
 +
 +  * Download current version((Version history is included in main file.)): [[https://wox.at/s/choose.txt|choose.php.txt]] (~ 14 KB; rename to e.g. ''choose.php'')
 +    * Example CSS [[https://wox.at/s/choose.css|choose.css]] (~ 1 KB; should give reasonable results out of the box)
 +    * Example XML for search engine addon: [[https://wox.at/s/choose-ffse.xml|choose-ffse.xml]] (~ 1 KB; needs to be adapted, of course)
 +  * View [[https://wox.at/s/choose.phps|source]] (with syntax highlighting)
 +
  
choose_php.txt · Last modified: 2020-12-12 20:41 by andreas