Hi, I am using the W3 Validator to validate a newly built site and it gives me some wicked errors - some of which I don't agree are errors. (this is an HTML issue, so I am posting it here instead of PHP, I don't think the file extension is the issue here, but the CODE in it - which is HTML!) I am using and external ".php" file for the menu buttons - I simply use "include" to put it on all pages and works 100 % fine, no errors. But W3 Validator doesn't like my links in it and gives me "400 error", Message: "This is usually the sign of a malformed URL that cannot be parsed by the server. Check the syntax of the link." The links in that file look like this (this is just an example): ../../../index.php ../../../vehicles/cars/index.php ../../../vehicles/motorbikex/index.php It seems like it doesn't like the "../../../", but I have to use it for deeper folders - so that the link can "jump" to the root source and then to other directories. I have tonnes of these and I don't think why they're regarded as "errors".
Generally speaking if you have to use ../ there is something terrifyingly wrong with your directory structure. The validator doesn't like it when you have more ../ than there are parent directories to point you at -- and some servers will even reject having that many of them as a way of blocking exploits. My own in-house CMS will reject them outright if detected in anything it has to process! Of course, if you want it based off the domain root, why not just use "/" instead of "../../../" ?!? href="/index.php" is usually far safer than "../../../index.php" Since poof, it will point at the root of the domain -- though that's usually avoided as it makes it non-portable -- but again that's why usually putting each page into it's own subdirectory is a bad idea. Particularly if you've got a perfectly good server language like PHP available. Put the pages into root, let them share a theme/style subdirectory of that. (particularly since sharing a common stylesheet can pre-cache sub-page appearance). Good rule of thumb is that if you have to use "../" you are likely doing it all wrong! Hence why when I'm making a site, the directory structure is thus: \ -- all .php or .html files the user can call go here. \downloads -- downloadable content, .htaccess to force download \images -- content images. \pages -- page body includes \sources -- php library files \static -- static includes \theme\default -- default template and CSS files \theme\default\images -- default template images \theme\themename -- template and CSS files for a theme \theme\themename\images -- template images for a theme and so forth -- that way all my SRC and HREF and url() will ALWAYS point down-tree so I never have to use ../, and it can always remain portable. Of course, I like the 'one index to rule them all' method of sitebuilding, so I actually only have index.php in the root and then use that to include from \pages each page -- after doing things like setting up gzip compression, parsing the URI, loading the template, etc, etc...
Hello, Thanks mate for your info. I am still trying to understand the "linking up to the root and down into a directory" issue. Actually I am building a travel site and it must have folders, because it's a bit complex. Like (these are just examples): guides/travelguidebooks.php guides/index.php guides/italy/milano/hotels.php guides/italy/milano/attractions.php guides/spain/barcelona/events.php guides/spain/barcelona/attractions.php news/index.php Suppose I want to link from "guides/italy/milano/hotels.php" to the general news page "news/index.php", then how do I do it? I try with: "../../../news/index.php" it works, but I understand from you that it's not the good way. If I try your way, then would it be "/news/index.php"? That doesn't work in all cases! In some situations it does indeed work, but when I try to link back up from a page that's 2-3 pages deep, then the "../../../" variant works. In this thread I read that the "../../../" is actually the right way to link back from the deep folders: http://stackoverflow.com/questions/12922391/linking-to-a-root-directory-folder-inside-html Basically the link on a page that's deep down in a 3rd sub-folder should point up to the root and the down to another page in another 3rd sub-folder. Like: point from "Las Vegas attractions" (Destinations > USA > Last Vegas) to let's say... "Monte Carlo attractions" (Destinations > France > Monte Carlo) I'm sure I'm getting many things wrong, but I'm learning I appreciate your help a LOT.
I'd probably either have a single index.php and a .htaccess to redirect ALL requests through it (what I normally do), or I'd have those be: guides_travelguidebooks.php guides.php guides_italy_milano_hotels.php So they aren't in subdirectories. Really, I'd use the first approach. If you insist on putting them into subdirectories, well, that stackOverflow article has the method I suggested, just using a single slash. Even if you did the single index.php the way I do it, you'd have to do it that way. So instead of: "../../../news/index.php" use "/news/index.php" a simple single slash points to the domain root. EASY. Likewise you could set the current root using the BASE element, but that would modify ALL URL's on the page, kind-of defeating the point of putting things into subdirectories. Pretty much to keep going how you're going, just replace all ../../../ with a single forward slash. That's the quick and easy fix. Let's say your file was in /guides/italy/milan/waterfront href="images/test.png" would resolve to /guides/italy/milan/waterfront/images/test.png href="../../../images/test.png" would resolve to /guides/images/test.png href="/images/test.png" would resolve to /images/test.png I'm pretty much assuming the last one is what you want.
I understand what you mean by the single slash, but it just doesn't work. Appears as "broken link". "/" doesn't work within files in subdirectories. It just doesn't link to the root. The only place where it works is within an "included PHP" that is actually on the root. I have that included PHP file for the main menus and bottom links.
Then something is horribly wrong as it should work from EVERYTHING, linking to the http root if it's in markup, CSS or scripting.. if you use it in PHP you're linking to the OS root, something different entirely. Wait, you're talking about INCLUDES in PHP?!? That's even more complex as you have to include the OS subdirectory at the start... which would end up something like: /var/www/sitename/web/guides/italy/milano/hotels.php Which is such a convoluted mess at that point I'd swing a giant axe at your directory structure and start over... possibly with a 'one index to rule them all' approach so that everything is run from the http root. (/ in http, /var/www/sitename/web/ in PHP) so again it all goes "down tree"... Somehow I was thinking you were just talking about client side. You really should NEVER do ../../../ in your PHP code, that's just sloppy broken garbage AND a possible security risk... since it can result in pointing up-tree past the http root!