|
Posted by Oli Filth on 09/09/05 19:19
Matthew Crouch said the following on 09/09/2005 14:48:
> Okay, I've come up with hacky workarounds every time I build an app, and
> now I just need some tips on how to do this correctly and efficiently.
>
> I usually have a head.inc and/or a constants.inc with definitions like
> (SITE_ROOT,$_SERVER['SERVER_NAME']."/myApp);
> (VERSION,"0.6.2");
> (APP_ROOT, SITE_ROOT."/".VERSION);
> (SMARTY_DIR, APP_ROOT . "/smarty");
> (ADMIN_DIR, APP_ROOT . "/admin");
>
> I'm sure you get the idea, and I bet you have them too. Some of these go
> fine, but many of them are all screwed up, esp. any dealing with
> -CSS (the stylesheets never seem to get included until my fourth attempt
> at writing the URL)
> -javascript (same)
> -images (sometimes ... it seems like different servers treat the initial
> slash in src="/thingy.png" differently?)
Assuming you're talking about URLs in the HTML, then the server will
never see something of the form "/thingy.png". Your browser sees it and
converts it to an absolute URL before sending a request to the server.
The basic rules:
* Anything starting "http://" et al. is an absolute URL.
* An initial slash in a relative URL means "work from the domain root",
i.e. "/thingy.png" translates to "http://example.com/thingy.png",
regardless of what the current path is.
* Anything else is treated as relative to the path of the *current* page
as seen by the browser.
* "./" means "use the parent folder", so if you're at
http://example.com/foo/bar/index.html, then "./thingy.png", translates
to "http://example.com/foo/thingy.png".
This is described formally in RFC 1808.
> Anyway, I always run into these snags in the midst of some project, and
> I figure out a hacky thing to just make it work. Now I want to get
> input/tutorial links, etc. on how to do this right:
>
> -do src= (script and img), action= (in a form), and href= (in a normal
> "<a" link, and in a <link stylesheet) have different behaviors, or am I
> imagining that?
No, they should all behave the same.
> -should I just build absolute paths for everything?
I would recommend not. It takes up more bandwidth to send (i.e. the HTML
file is bigger), makes the HTML harder to read (for humans, that is!),
and if you ever change domain or the root folder, then you'll have to
change all your links (unless you build the links dynamically).
--
Oli
[Back to original message]
|