Reputation: 207
My links look like
<link rel="stylesheet" href="/utils/style.css" />
I had my website in main dir so it wasn't a problem but when I moved all my files to /dir/ I set up base tag :
<base href="http://localhost/dir" />
But it doesn't work. It works perfectly fine though if I use online URL to my website
<base href="http://www.my-website.com" />
what's wrong ?
Upvotes: 7
Views: 18891
Reputation: 31
Pawel you can use base tag as :
<base href="http://localhost/dir/" >
It should work perfectly on localhost whereas in case of live website you can use directly the root extension :
<base href="/dir/" >
Upvotes: 2
Reputation: 317
Pawel, answer to your comment "So basically problem if my urls start ...": Think like this and it will all make sense: If your URL:s in links are absolute, then your browser uses those URL:s as they are and sends them to the server, because in that case the browser has no need to transform them into absolute paths before sending them to the server. So that's the reason the browser does not care whatever (illegal or not) path you have specified as href
in the base
tag. (The browser already has the information it needs.)
I recommend you to use the scheme I've written below. That scheme works well for me. Note that the URL:s you specify in <a href> <img src>
etc. should all be relative (i.e. not begin with a slash) like so: mytoplevel-dir/mysubdir/myresource.xxx
or, if the resource is located right under the root, like so: myresource.xxx
. Note that you should not (need to) go up any level(s) (../subdir
or ../myresource.xxx
). Now, when you have made your URL:s relative, the href
of the base
tag starts to matter.
About making the code to suit both your local development environment and your live web server): Just use PHP to dynamically generate the path to use in the base
tag. To do so, you may use $_SERVER["SCRIPT_NAME"]
and find any context path (i.e. directory that is specific for your website on your local server). This works well (I use it). Example:
<?php require_once('../common/php/URL_Manager.php'); ?>
.
.
<base href="<?php echo URL_Manager::getCurrentServerAndContextPath(); ?>" />
.
.
My understanding is that <base href>
shall have a fully qualified path that ends with a /
(forward slash), whereas relative paths shall be used elsewhere e.g. in <a href>
, <img src>
and <link href>
(for internal links that is).
Without <base href>
, the browser resolves each relative path encountered by using the path as seen in its address bar, into an absolute path -- which is the path sent to the server.
With <base href>
, the browser resolves each relative path encountered on that page by using the absolute path stated in the <base href>
, into an absolute path -- which is the path sent to the server.
Note that either way the server receives only absolute paths.
Finally for completeness, I should mention that when you use the base
tag like I propose above, each anchor link you may have need to be supplemented with the current path, like so:
<a href="<?php echo URL_Manager::getCurrentPath(); ?>#menuBookmark">
instead of just:
<a href="#menuBookmark">
Upvotes: 0
Reputation: 10260
Because dir is a directory you have to finish with a trailing /
Try
<base href="http://localhost/dir/" />
<link rel="stylesheet" href="utils/style.css" />
EXTRA
It may be neater to map your local host to a real url this would solve your issues.
Should be something like this in your hostfile
# localhost name resolution is handled within DNS itself.
127.0.0.1 dev.my-website.com
Upvotes: 0
Reputation:
Dir is a directory therefore you need to add a / after it, also you don't need to use base to have clean URL's... If that's what your trying to do.
<base href="http://localhost/dir/" />
Upvotes: 4