homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

base url

 10:47 pm on Jul 8, 2012 (gmt 0)

Is there some way to keep links static for each page?

e.g. if I want to link to something like:
<a href="http://www.domain.com/<?php echo $row['url']' ?>

Is there some substitute I can use instead of domain.com?



 12:10 am on Jul 9, 2012 (gmt 0)

On most Apache-based servers, if you wanted to use complete URLs, you could use something like:

<a href="<?php echo $_SERVER['HTTP_HOST'] . '/' . $row['url']' ?>

If relative URLs are okay, you could simply use:

<a href="<?php echo '/' . $row['url']' ?>


 4:04 pm on Jul 9, 2012 (gmt 0)

I'd go with the latter, and clarify, that means domain root relative, which will almost always work. Too often links are coded like so,

<a href="script.php">

Which is "just relative to wherever you are." This is fine if the resource is always "in the same directory." When you get to using rewrite rules and other conditions, it's not always the case. The leading slash always means "start at the domain root."

A bonus, that works whether you're currently on an SSL page or a non SSL page, subdomain, even a second site.

<a href="/script.php">

can be

http ://www.example.com/script.php
https ://example.com/script.php
http ://mysubdomain.example.com/script.php

http ://www.some-other-example.com/script.php


 8:20 pm on Jul 9, 2012 (gmt 0)

Ok, so just insert a slash in before the url. Is that really enough? Rather than setting up a global variable.


 4:00 pm on Jul 10, 2012 (gmt 0)

Yeah just that simple. :-)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved