put this at the very top of your template:
{if segment_4 == ""} {exp:channel:entries channel="channel_goes_here" entry_id="{segment_3}"} {redirect="{site_url}/{segment_1}/{segment_2}/{segment_3}/{url_title} {/exp:channel:entries} {/if}
This will check if you installed the fourth segment, if it does not occupy the third segment containing your entry_id, passes it to the channel record tag, which returns the desired url_title. With this url_title you can easily redirect to the correct page.
source share