You need to 'url encode' the url item in the query string ("http://www.domain.com/?p=1"). Your encoded url will be
index?url=http%3A%2F%2Fwww.domain.com%2F%3Fp%3D1
That could then be rewritten easily to
/url/http%3A%2F%2Fwww.domain.com%2F%3Fp%3D1 without too much trouble.
To get the encoded url string you can use JavaScript's
encodeURIComponent function or PHP's
urlencode function, to name a few.