URI Router

yaur -- yet another uri router
written and distributed under the BSD License

All the routing is now done via the settings in the router.conf, the idea was "to decoupling of [the] serve logic from Apache - to be server-agnostic".

The whole idea is to take URI path, _GET/_PUT/_SERVER data and other meta-data, then process rules in this file sequentially, which results in dispatched handler to process query, and all arguments extracted from URI for further usage by those handlers (e.g. you can extract parts of URI to _GET vars, etc.).



1. 'Language' guide

Every line of code consists of regex which matched against URI, and actions, all separated by whitespace.There are no possibility to include whitespace in regex or action. Regex will be matched against URI, on success all actions executed, on failure - we go for next regex. Lines without regex is continuations for same regex, and will be executed sequentially if regex matched.

Every single action can succeed or fail. If any one fails - all variable assignments made by THIS line (even before failed action) is discarded, and we go to next line.

Two control actions exist (all action line must succeed for them to act!)

_next! -- jump to next regex (skip next action lines with empty regex)
_ok! -- search terminated with success

If no _ok! is executed ever - search fails and 404 is emitted.

That's all on control flow.

1.1. Main regex

PCRE regex syntax -- usual php preg_* regex syntax apply, including delimiters and options after trailing delimiter. Convenience macros (defined by 'define' line, or supplied by WackoWiki) expanded before matching. Used as {macro} - to be referenced as $1-vars then, or {var=macro} - for inline assignment. Macros cannot be used in ~-regexes.


$0..$9 - fields matched by main regex. $0 is complete match, $1 and later - corresponding (...) parts
$a..$j - as $0..$9 but set by sub-matched (by ~ operator) patterns
Gname - _GET[name]
Pname - _PUT[name]
Sname - _SERVER[name]
others - local variables

Predefined vars:

_tls - 1 or 0, tls session
_uri - parsed URI (it is matched against main regex, but can be changed by assignment)
_rewrite - 1 or 0, mod_rewrite active

Usage of undefined variables considered a failure (if not masked by @ in VALUE expansion, see later)

Similar format used for all actions (not all fields apply for every action, and just ignored):


Value is a string, with expanded variables. expansions:

$0 .. $9 / $a .. $j -- see above
@$... format can be used to mask undefined variable error
$$ - replaced by $
$@ - replaced by @


FUNCTIONs can be used: tolower | toupper | int
var?=$2 -- assign if not set
var! == var=1
dbg=$1,$2,$3 -- Ut::dbg printer ;)

Pattern matching:

var/regex/i -- sets $a..$j on success
varhashid:[1-9] -- hashid expansion, into $a...


FUNCTIONS can be used: int -- both args converted to int before comparison
var==12 var!=12 var:int<12 var>12 var<=12 var>=12


var? -- isset
var- -- unset

2. router.conf

//define	{method}	name|name|name		// predefined by wackowiki
define		{hashid}	[0-9a-zA-Z]+
define		{i}		[0-9]+
define		{h}		[0-9a-fA-F]+
define		{a}		[0-9a-zA-Z]+
define		{w}		[\w]+
define		{}		[^/]*
define		{*}		.*?
define		{**}		.*	

//`^{hashid}$`			$1~hashid:2 Gone=$a Gtwo=$b all=$0+${Gone} _ok! _tls!=0 _method~/g(e)t/i BIN:tolower=$b Pln=${_line}
//				desc=$0 term:tolower=MyMethod term!= Gmethod=show _ok!

//				Op!~/^diff$/i &next!
//				$1~hashid:2 Gone=$a Gtwo=$b Gmethod:tolower=${Op} _ok! // test

// ...

//				page=$1x$2 method=hashid redirect=301 _ok!

//				method=show _ok!	

2.1. Example


//define	{method}	name|name|name		// predefined by wackowiki
define		{hashid}	[0-9a-zA-Z]+
define		{i}		[0-9]+
define		{h}		[0-9a-fA-F]+
define		{a}		[0-9a-zA-Z]+
define		{w}		[\w]+
define		{}		[^/]*
define		{*}		.*?
define		{**}		.*

	SPATH_INFO!= _uri=${SPATH_INFO} _next!		// if PATH_INFO available - use it
	_rewrite==0 _uri=@${Gpage} Gpage-		// when rewrite mode is off - replace _uri by page _GET variable

`^/*{_uri=*}/*$`					// trim _uri of beginning & trailing slashes
`^index\.(php|html)$`					_uri=

'^'							route=static age=30 static=${_uri}

`^robots\.txt$`						_ok!
`^sitemap\.xml$`					_ok! age=0

`^(theme/{}/css|theme/_common|admin/style)/{}$`		_ok!		// css
`^image/(wikiedit/)?{}$`				_ok!		// icons
`^theme/{}/icon/{}$`					_ok!		// icons
`^js/(lang/)?{}$`					_ok!		// js
`^file/(global/){}$`					_ok!			// global uploads
`^file/thumb/{}$`					_ok!		// global thumbs
`^setup/(image|css)/{}$`				_ok! unlock=1	// setup inlines
`^xml/{}$`						_ok! age=0	// feeds

'^'							_ok! _install!=0 route=install unlock=1
							session=1 age- static-

`^\.freecap$`						_ok! route=freecap session=2

'^'							engine=1 route=wacko

`^admin\.php$`						_ok! route=admin

	$1~hashid:2 page=$ax$b method=hashid _ok!

`^(|{page=**}/){method}(/.*?)?$`ii			method:tolower=$3
							method==file session=2

// catch-all
`{page=**}`						_ok! method=show	



3. Rewrite

Mode rewrite is now set in the Settings class at runtime, regardless what you set in the config, this follows again the server-agnostic idea.


// if .htaccess tell us actual info on mod_rewrite status - use it
if (getenv('HTTP_MOD_ENV') === 'on' && AUTO_REWRITE)
    $this->rewrite_mode = (getenv('HTTP_MOD_REWRITE') === 'on');

For debugging purposes, the auto-rewrite setting can be turned off via constants.php.

  • const AUTO_REWRITE = 0;
  • rename .htaccess to dist.htaccess

3.1. The .htaccess file

# No user serviceable parts inside
# If you want to fix anything by tuning htaccess - you're possibly on the wrong path

<IfModule mod_env.c>

<IfModule mod_rewrite.c>
	<IfModule mod_env.c>
	RewriteEngine on
	RewriteRule ^ index.php [QSA,L]

<IfModule !mod_rewrite.c>
<FilesMatch \.php$>
	# Apache 2.4
	<IfModule mod_authz_core.c>
		Require all denied
	# Apache 2.2
	<IfModule !mod_authz_core.c>
		Order Allow,Deny
		Deny from all

<FilesMatch "^(admin|index)\.php$">
	# Apache 2.4
	<IfModule mod_authz_core.c>
		Require all granted
	# Apache 2.2
	<IfModule !mod_authz_core.c>
		Order Allow,Deny
		Allow from All
		Deny from None

3.2. Example

  • httpd.conf LoadModule rewrite_module modules/mod_rewrite.so
  • config,php 'base_url' => 'http://localhost/wiki/',
  • page tag: RecentlyCommented

3.2.1. ON

 LoadModule rewrite_module modules/mod_rewrite.so	

result: http://localhost/wiki/RecentlyCommented

3.2.2. OFF

 #LoadModule rewrite_module modules/mod_rewrite.so	

result: http://localhost/wiki/?page=RecentlyCommented

It works without index.php/ in base_url, the URI router appends ?page= if mode_rewrite is OFF.

4. Known Bugs and Limitations

5. Copyrights

© Serge Terekhov, 2016-2019

6. Contacts

If you found any bugs in the URI Router, please inform us — Bugtracker.