Forum Moderators: open
Here's the issue: I like to use absolute paths (what some people call "root relative" paths) for links, includes, images and so forth. My links look like this: <a href="/link.html"> My img tags look like this: <img src="/img.jpg"> And so on. Sometimes it's necessary to use these paths, such as when you're calling includes and you need the links to work whether the calling page is at the root level or 3 directories down. And scripts often require the absolute path. I am not open to switching to relative paths (../).
I use a host that does not provide a dedicated IP with each domain -- thus, when setting up a new site, I have to use this format until the domain name gets propagated: [host.com...] Of course, my absolute paths don't work in such a scenario because the server's file structure doesn't map to the virtual domain.
I really need to be able to upload the site and test it thoroughly *before* propagation -- that means I need all my links to work, all my scripts to work, forms, includes, etc. and I'd prefer not to hard-code the entire "http://host.com/~user" into every link and image on the site, just to have to change all of them again after the domain propagates. Too much room for errors that way.
My question: Would the href base element solve this issue? If I put <base href="http://host.com/~user"> in my pages, can I use my absolute paths and have the browser call all of those elements with the ~user part as part of the call? Then it would be a simple matter to change or remove that once the domain name propagates.
Or is there a better way?
However, you can bypass the propagation problem more easily by simply editing your "hosts" file on your PC. The location of the file depends on your operating system version, but just do a search for the file "hosts" (without quotes, and no file extension). Open the file with Notepad, and you'll normally see one entry for 127.0.0.1 localhost. You need to add the following:
123.456.789.012 www.example.com Where the numbers are replaced by your shared IP address of the server, and www.example.com with your domain name.
Using hosts will bypass the DNS lookup when you visit your domain, so you can see your site on the server before it is available to others.
It sounds like the base href may do the trick, though. I've figured out that I can set my php include path in .htaccess, which lets me set (and change) that by simply editing one file. Then I can put an include in all my files that will include the base href. Once the domain propagates, I simply edit the .htaccess file and the base href file, and the site is ready to go. No need to make sitewide changes to all the pages, which is what I really really want to avoid. Theoretically, this should work. In actual practice, well, I'm glad I'm working on a very small site right now to experiment with.
Wish me luck. I'll report back on how this works.
What I did was:
1. Set my php include path in my .htacess file. The include path won't change even after the domain propagates, since it's file system based. Now all my included files are included successfully without regard to the virtual document root vs the filesystem document root.
2. I generally use an include in my document head to include the standard meta and head tags that don't change from page to page -- no MS toolbar, stylesheet, etc. So in my "meta" included file, I put the base href tag.
3. The base href in the included file initially uses the [IPaddress...] that I have to use until the dns propagates. Once the dns propagates, I change the base href in this single included file to the actual domain URL, and voila! Everything still works.
The trick was that the base href needs to include the trailing end slash. Without the trailing end slash, this doesn't work. (At least, it didn't for me.) All my href links and img tags don't use the beginning slash that you would normally use to start at the site root -- because the trailing slash in the base href already puts it in the right place.
I'm so happy to have found an easy solution to something that has periodically been a problem for me.