Forum Moderators: coopster

Message Too Old, No Replies

relative page urls

Relative page urls

         

Olly333

8:39 am on May 11, 2009 (gmt 0)

10+ Year Member



Hey guys,

Is there a piece of code that can be put a the top of a page, that would make all links relative to the root directory instead of the current directory?

basically when i put


<?php include"head.php"?>

on page myurl.com/dir1/page1.php
it includes myurl.com/head.php
and not myurl.com/dir1/head.php

this would also have to change a href inside head.php, which would usually link to myurl.com/dir1/page2.php
to link to myurl.com/page2.php

cheers

[edited by: Olly333 at 8:40 am (utc) on May 11, 2009]

g1smd

8:50 am on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Look at the BASE tag.

However, I prefer to start all links with a single slash and state the full folder and file path every time.

I do not include the domain name in internal links as I have redirects to fix canonicalisation already installed on the server.

Olly333

8:58 am on May 11, 2009 (gmt 0)

10+ Year Member



i don't put full urls either i just wrote it to show where it is supposed to be

uh what is canonicalisation and how do you redirect them?

penders

9:12 am on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hhhhmmm, I don't think you mean (HTML) 'links' ...? But paths to included PHP files judging by your example? In which case you can make them absolute...
<?php include($_SERVER['DOCUMENT_ROOT'].'/head.php'); ?>

Or, you could consider adding the webroot to your include_path, so you would not need to explicitly state the path to your included files each time.

Olly333

9:26 am on May 11, 2009 (gmt 0)

10+ Year Member



yes!
but will that for example, because the actual file is in a sub dir, will it change the style.css call in the header.php to ../style.css?

penders

10:26 am on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...but will that for example, because the actual file is in a sub dir, will it change the style.css call in the header.php to ../style.css?

Ah, yes that could be a problem if head.php includes relative links to your stylesheet(s) and head.php is being included into pages at different depths in your directory tree.

As g1smd suggests, you should be able to use root-relative links to your stylesheets, by preceeding your URL with a single slash, ie:

href="/styles/style.css"

By using root-relative links in this way will mean that all links use the same path regardless of where the file is located in the directory tree.

g1smd

10:29 am on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That is exactly what I meant. :)

penders

3:35 pm on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yep. :)

Olly333

6:29 am on May 12, 2009 (gmt 0)

10+ Year Member



Ok cool that works now thanks guys