"/" in the href links

I am confused regarding the “/” in links while writing PHP code…Could anyone tell which one of the folowing is correct:

<a href=“/xyz/abcdef.php”>

OR,

<a href=“xyz/abcdef.php”>

What you posted there was HTML code. And either form is fine; they just mean different things.

This…

<a href=“/xyz/abcdef.php”>

…is an absolute link, and means that file abcdef.php is to be found in the folder /xyz/ which is located in the root folder of the site.

While…

<a href=“xyz/abcdef.php”>

…is a relative link, and means that the file abcdef.php is located in the folder /xyz/ which is located inside the current folder (i.e. inside the folder containing the page on which the link appears).

SO, the first example is more useful, because you can use that link anywhere in the site and it will always point to the one same /xyz/ folder within the root folder. The second link will only work on pages whose containing folder has a subfolder called /xyz/.

Make sense?

Yesh

[ot][i] Stomme poes eats the chocolate.

  • Stomme poes purrs[/i]*[/ot]

That’s actually a good point Poes because you could get all kinds of crazies linking to non-existent directories/paths like; SP /FoRums because the site admin though it would be “cool” to accept every case-typo thrown.

Off Topic:

Stomme poes catches the chocolate bar. Stomme poes eats a chocolate bar.
#chat
Stomme poes is eating noisily.
#chat
Stomme poes purrs.

See that is what you are supposed to do with a poes if you make it angry it will ‘YOWL’. :wink:

As it so happens I use case sensitive markup too, which in turn affects the CSS and so-forth.

i remember i have come across a thread where Stomme poes responded positive to chocolate (and badly to the lack of it). are we to assume that, if we throw in a little bit of yummy chocolate, she will at least consider the possibility of using NC :slight_smile:

That’s a bad idea IMO Stomme, do you think the average Internet user will recognise the difference between cases and more to the point, will they be more likely to make typo’s as a result of not remembering the case required?

They get what they ask for. If they want what’s in Images, they get what’s in Images. I strongly disagree in setting NC for file paths. But I don’t use IIS or anything Windows.

If they make a typo, they get a 404… just like they do on the whole rest of the internets.

Well, I think of it as absolute in that it always points to the same location.

Why change it? Using NC means that it works for both upper and lower-case paths. :slight_smile:

Yea, I don’t get calling /path/file.ext an absolute path, I call an absolute path something that includes the full address (protocol, subdomain, domain, TLD, and path) :stuck_out_tongue:

Thanks Alex. Obviously I don’t understand this rewrite stuff. I don’t see why you don’t just use /images/image.jpg in your links anyway (which I call an absolute path, BTW).

A quick example of using htaccess to link to specific pathnames:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^images/(.*)$ /images/$1 [NC]
RewriteRule ^style/(.*)$ /style/$1 [NC]
RewriteRule ^feed/(.*)$ /feed/$1 [NC]

With this in the htaccess file, if you link to /images/image.jpg (as a relative path), no matter how deep the folder structure is, it will still recall the /images/ location from the very BASE directory where the file is stored, thereby you can use relative links that don’t worry about how deep the structure is, it’ll just work. :slight_smile:

That’s a bad idea IMO Stomme, do you think the average Internet user will recognise the difference between cases and more to the point, will they be more likely to make typo’s as a result of not remembering the case required? I stand by my usage of allowing both cases to the same path, usability is paramount. :slight_smile:

Only if you don’t plan for it from the start. I find using either to be impossibly restrictive for testing in subdirectories, testing locally, moving the site between locations if need be…

If I DO use ‘/’ or direct tree links, it’s usually automated and extracted from the $_SERVER vars.


$data['linkHome']=str_ireplace('index.php','',$_SERVER['PHP_SELF']);
$data['themeDir']='themes/'.$data['theme'].'/';
$data['linkRoot']=$data[linkHome].(
	$data['useModRewrite'] ? '' : '?'
);

Mind you, that’s for routing ALL requests through index.php; either via GET or using modRewrite. (and exploding $_SERVER[‘REQUEST_URI’] in both cases!)

Which is how url’s in my system end up either:
http://somesite.com/page/0 – modrewrite on
or
http://somesite.com/?page/0 – modrewrite off

(I actually have a trap to use $_SERVER[‘PHP_SELF’].‘?’ for windblows since iis can’t handle the shortform)

I’ve never once had to use …/ – but then I don’t go nuts slapping every html file in it’s own directory like some people. (because having 1 file in it’s own directory is cleaner than just putting that file in the root HOW exactly? Laugh my ass off every time I see someone do that)

If it’s markup, it should be ‘executing’ in the root (or relative root if running from a subdirectory) so any includes would be based off that. Javascripts, even if called from subdirectories would execute as if they were in the same directory as the html when it comes to pathing so no headaches there. Content images (IMG tag) would also path off the root, while CSS images would always path off the CSS location; and if you are practicing separation of presentation from content there is NO reason for the CSS to be calling files from anywhere but down-tree of itself, even if multitheming in multiple directories.

But it seems like a lot of developers don’t even THINK about their directory structures when making their systems; hence the spaghetti linking. (just as bad as spaghetti coding IMHO). A bit of planning before laying down a single line of code goes a long ways.

I use both and I’ve never had a problem with either. Only allowing links to ‘downstream’ folders and files is, for most sites, impossibly restrictive and will cause you far more headaches than allowing upstream and root links.

The widest-used convention on the web is that ALL addresses, whether web or email, are in lower-case

and I use this convention in anything I build for a client. But my personal stuff takes advantage of Unix filesystem rules. And I do want someone looking for
FOO.html to get a 404. That is not the file name. Using [NC] in your rewrites isn’t going to help: anything not rewritten WON’T have case-insensitivity anyway. They are simply two different files/dirs/paths entirely.

Making things user-friendly doesn’t extend to pandering excessively to typos. Just as I won’t get the correct page if I type in
sitepoint.com/froums
(and I shouldn’t)
I should also not get the correct page if I type in
sitepoint.com/Forums
and… whaddaya know, I get a 404. As I should. What’s the first thing any user who’s typed in a URL going to do if they see a 404? They’ll check the address bar.
Would you argue Sitepoint should add [NC] to all their rewrites as well?

are we to assume that, if we throw in a little bit of yummy chocolate, she will at least consider the possibility of using NC

No, but I’d eat the chocolate : )

I want to make my sites as easy to use, and as easy to maintain. The widest-used convention on the web is that ALL addresses, whether web or email, are in lower-case. The second most common convention is that they are case-insensitive (as is automatically the case for domain names). Most people are so habituated into seeing URLs in lower-case that they will instinctively type them in lower case, even if they have seen it written with capitals. Going against those conventions and having case-sensitive folder and file names featuring capital letters seems just perverse - you are setting up pitfalls for people visiting your site, and you are setting up pitfalls for yourself as you maintain the site.

all relative paths are there because developers (might) need them, at one point or another. not all projects have the luxury to be unrelated.

no matter how careful you plan, there comes a site with a more complex structure, with submodules, when going to root or upstream to locate resources is a must.

also, in order to eliminate redundancy, when a resource is shared across projects, the same relative path proves to be useful again.

just because one can’t find a use for a thing, that doesn’t make that thing useless in general :slight_smile:

using only downstream relative path seems a bit unrealistic to me. shared resources on a web server need to be unique, otherwise that web server would look like a resource spaghetti :slight_smile:

change [NC] to [L] though.

For me, images == /images, so such a rewrite wouldn’t do anything for me.

I’m the sort of dolt who needs an example to understand this. :frowning:

To make it even clearer, let’s say out anchor is served from this url.

“attachments/” would point at

“/attachments/” would point at
http://www.sitepoint.com/attachments

As a rule of thumb I try to avoid putting directory structures in my code that would rely upon either “/” or “…/” – the former ruins portability and the latter always just feels like sloppy coding to me.