Jump to content

Full domain in page links in the source code


brandy
 Share

Recommended Posts

Hi!

I have been trying different things for hours, but it doesn´t work as I think it should.
I want to have the full domain as page links in the source code, for example https://domain.at/site1

One try was:

<?php echo $config->urls->root?>site1

This works on hovering the link and looks like the example above.
But in the source code it´s <a href="/site1"....

I also tried

<?php echo $config->httpHost?>/site1

This ends in <a href="domain.at/site1" in the source code, but on hovering the link it shows https://domain.at/domain.at/site1

How to I get in the right way?
Thanks a lot!

Link to comment
Share on other sites

The httpUrl property/method will provide you with what you want, whether referring to a Page, PageImage, or PageFile object. The browser should the full URL when you hover in the status bar area simply because that's what it does, but you are correct in that you need to view source to verify what's being output in code.

Typically you don't actually want to hardcode the full URL into the rendered code as it's a bit slower for the client (web visitor's browser) to negotiate the resource. If they're already on your domain, you just refer to the relative path of objects/pages/files on your domain; it's a tiny bit more efficient, and as you've already noticed, the browser automatically identifies/assumes the correct location anyway.

If you're running a single website on multiple domains but want to redirect visitors to a specific domain, you could use redirects on the server instead of hardcoded domain-based links, but that doesn't mean you can't do what you're doing either. As for SEO benefits or detriments to this, aside from the speed hit, I'm not sure if there are any.

If you plan to generate JSON-LD, Canonical URLs, XML Sitemaps, PDFs, or other potentially downloadable files from your content, that would be one area that could benefit from absolute URLs if you didn't want to do extra work, later on, to adjust the URLs on export.

Ultimately though the choice is yours depending on context, needs, and desires - and ProcessWire provides you with that choice! $page->url or $page->httpUrl. To get the current website domain, you'd likely want either $input->httpHostUrl();, or $pages->get(1)->httpUrl; (or $pages->get('/')->httpUrl; they'll both retrieve the home page's absolute path; the home page in ProcessWire always has an ID of 1).

Take a gander at the searchable ProcessWire API. For the above, you could find them by searching, "host" (without quotes).

For identifying other paths, run a PHP print_r() on $config->urls. ? For reference if anyone else is reading this over and needs it, here's a simplified breakdown of relative vs absolute paths in URLs: https://www.conductor.com/academy/urls/faq/absolute-vs-relative/

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...