views:

115

answers:

3

Is the html <base> tag safe to use in terms of browser support? Or should I generate a root path with PHP which i then add like this <a href="<?=BASE?>somepage.html">somepage</a> which makes up a absolute url.

using the base tag like this <base href="<?=BASE?>" /> I am then able to use links like this

<a href="somepage.html">somepage</a>

now I am fully aware that it would be much easier to just do this without using the base tag:

<a href="/somepage.html">somepage</a>

but how do I test locally then with a base url of http://localhost/testsite/ ???

edit: thanks guys, your the people who make the stackoverflow community so great :)

+2  A: 

I like the first option, outputting a base directory in PHP tags

<a href="<?=BASE?>somepage.html">somepage</a>

the very best. This makes your site independent from which directory it is installed in (you may not be able to change virtual host settings on shared hosting packages). It is also easy to outsource image files to a different server/subdomain if ever need be.

<base> is safe to use in terms of browser support, but I personally recommend strongly against using it for reasons of code maintainability. <base> changes the rules for how URLs are processed; you need to keep the base in mind for all relative URLs; and is very easy to overlook. From a programming perspective, it feels like a dirty fix.

Pekka
so your saying i should be adding a base directory to each and every link?
YuriKolovsky
@Yuri I do it that way, yes, but I develop apps that need to be able to be deployed in subdirectories like `/subdir/appname/index.html` so it's either that or `<base>`. You don't always need that. Jimmy's approach of simply correcting the virtual host will work fine if your issue is limited to your local testing environment, but your application or site won't be able to easily use a sub-directory that way - the paths will be hard-coded to the root directory.
Pekka
answer accepted, its the way ive been doing it for some time, i just thought base was a way to reduce the html use, but it seems there is no true way out, except if i use a mix of this and jimmys trick.
YuriKolovsky
+1 Just realized you are discussing subdirectories. I'll leave my answer up anyways as it contains some details some might find useful.
webbiedave
@webbie yup, I think we're basically making the same point. But that doesn't hurt as it's a good point :)
Pekka
+1  A: 

My advice would be to use absolute URLs beginning with a slash, and just set up a virtual host that uses /localhost/testsite/ as its document root. Then you could leave the absolute URLs and just access your site at something like http://testsite/ locally.

Jimmy Cuadra
oh, i knew there was some simple solution to this! If i only knew how to set up a virtual host... ok back to google then. But what if i want to change the domain/subdomain?
YuriKolovsky
Your link URLs won't change no matter what hostname is used to access the site. That's the beauty of this method. When you create a virtual host you can set the hostname it will use. If you ever need to change it, it's as simple as changing the configuration file.
Jimmy Cuadra
valid point, now i'm thinking that i can use all 3 linking methods.base links, absolute links and relative links.
YuriKolovsky
I can make BASE = '/'; in which case your trick comes into play.
YuriKolovsky
+1  A: 

You've definitely given this some thought but I would like to throw one more consideration into the mix. If you are writing a web application, you should construct it in such a way that you can install it into any sub-directory in the future and it will continue to work with little change.

This means that href's, src's, action's and even HTTP Location headers will need to be aware of such a change. That's why I recommend prepending your uri's with <?php echo SITE_BASE ?> or whatever you want to call it.

Debate can rage on as to whether SITE_BASE should contain a trailing slash.

webbiedave
thanks for the answer, this clears me up showing that base is not the way to go.
YuriKolovsky