How can I enter an internal link that works for different path lengths and keeps working when I upload my site to the live server?
Say I have a page here http://localhost/site1/web/page1
and I want to create a block in the footer that has a link to this page.
If I use a relative link (e.g. page1
)
It will work when I'm on this page ...
http://localhost/site1/web/
But it won't if I'm on a multi segment path, e.g ...
http://localhost/site1/web/about/terms/
If I create a server-relative link (e.g. /site1/web/page1
)
It will not work when the server changes ...
http://mysite.com/
(I develop the site locally on my PC and when the site is finished I upload it to my hosting site).
Also, it will not work if I wish to clone the site...
http://localhost/site2/web/
Absolute paths (e.g.http://localhost/site1/web/page1
)
will also not work when I start running the site on the live server.
I tried the module linkit. It works for the above cases but only for some types of links (e.g. I couldn't get it to work for links to views).
Module pathologic fixed this issue in Drupal 7 but is not yet ready for Drupal 9.
I got the best results using the php module (it supplies a filter that you can add to a text-format). But I believe using this module is frowned upon.
<?php
use Drupal\Core\Link;
use Drupal\Core\Url;
print(Url::fromUserInput('/node/1')->toString());
?>
Am I missing a better alternative? (It would be handy if there was a way to make links that are relative to the drupal 'web' directory.)