It was little strange but it was happening. I have found solution here
Solution was in .htaccess
file and not in php. It required to change in the .htaccess file for line
RewriteRule ^ index.php [L]
to
RewriteRule ^(.+)$ /index.php [L]
The same was working fine on one server but not on another.
I want to understand this behavior. Why this was happening?