fixed URL generation and compatibility for older versions of PHP