I have a page on my site that fetches and displays news items from the database of another (legacy) site on the same server. Some of the items contain relative links that should be fixed so that they direct to the external site instead of causing 404 errors on the main site.
I first considered using the <base>
tag on the fetched news items, but this changes the base URL of the whole page, breaking the relative links in the main navigation - and it feels pretty hackish too.
I'm currently thinking of creating a regex to find the relative URLs (they all start with /index.php?
) and prepending them with the desired base URL. Are there any more elegant solutions to this? The site is built on Symfony 2 and uses jQuery.
Well, not actually a solution, but mostly a tip...
You could start playing aroung with ExceptionController.
There, just for example, you could seek for 404 error and check query string appended to request:
The other solution would be to define special route with its controller for such purposes, which would catch requests to
index.php
and do redirects and so on. Just defineindex.php
inrequirements
of route and move this route on the top of your routing.Not a clearest answer ever, but at least I hope I gave you a direction...
Cheers ;)
Here is how I would tackle the problem:
See it working
You can override the
base
tag by puttinghttp:\\
in front of the link. That is, give a full url, not a relative URL.