Proper way to set up rewrite with Wp

See flush_rules in Codex:

Because this function can be extremely costly in terms of performance, it should be used as sparingly as possible – such as during activation or deactivation of plugins or themes. Every attempt should be made to avoid using it in hooks that execute on each page load, such as init.

Only flush rules when they change, not on every page load.