Use str_replace instead of substr and strlen to remove the catalog path from $_SERVER['SCRIPT_NAME']