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 somepage which makes up a absolute url.
using the base tag like this <base href="<?=BASE?>" /> I am then able to use links like this
somepage
now I am fully aware that it would be much easier to just do this without using the base tag:
somepage
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 :)
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.
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.
I like the first option, outputting a base directory in PHP tags
somepage
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.
Comming back to this question 2 years later, I now realise that the best way to handle absolute URLs is to put the URL into a wrapper function, for example like this:
somepage
the wrapper function then gives you full control over the URL handling
function url($url){
if(strpos($url,'/')===0){
return 'http://localhost/testsite'.$url;
}else{
return $url;
}
}
Related
So basically, what's the difference between the two approach:
<img src="<?php print base_url(); ?>img/test.png" />
<!-- and -->
<img src="/img/test.png" />
I'm having the same result on my website. Might as well remove the URL helper ($this->load->helper('url')) on my controller to cut extra process since they're just the same.
If there is an advantage of adding the helper at all, please let me know.
The difference is if you ever want to put your application in a subdirectory.
If what was / now becomes /application/ all your site root references will break.
It just makes your application free to be installed anywhere and won't require being on the site root.
In my experience with CodeIgniter, using base_url() is a best practice. It ultimately guarantees that your links will be correct (similar to the way adding ../ works when the folder you need is 1 level higher). Also I have had problems in the past with CodeIgniter adding the url in config[base_url] twice if I forgot to put http:// in it. Simple mistake, but it can throw the whole project off. So, basically the difference is that it guarantees the path is the right one, but isn't necessary at all times.
There is no difference if you're using root path of domain.
It's kind like a feature that if you're using it you can migrate easier to any domain or inside directory path.
One more advantage is when you're developing API or RSS feed, the image path or anything else will be written as complete URL so the consumer can use it easily.
I've discovered another thing usin site_url(). It is the only way I found to set a / at the end of my urls.
Eg : will result in http://domaine.tld/controller
On the other hand http://domaine.tld/controller/
hope that helps somebody
The following mod_rewrite rule is causing my website to behave odd I type localhost/signin/.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^signin/?$ access.php [NC,L]
It causes all of the attached files (images, css, php, js) with relative links not to load? However when I type localhost/signin all the attached files with load like nothing is wrong. I think this happens because /signin/ is treated like a directory and all relative links will look into an non-existence directory for all the attached files.
Edit: I figured out a solution but it involves muddying up my markup which is not what I want. Look at my answer below and you'll see.
However, I want these attached files to be ignored by the rewrite rules.
The problem
The problem you have is (after clarification) not related to the use of rewrite rules. Please edit your question to accurately represent your problem.
Almost certainly because you have references such as:
<link rel="stylesheet" type="text/css" href="css/foo.css">
<script src="js/foo.js">
which means the route used to find the css/js files is dependent on the directory (virtual or real) of the current request.
If the current request is /foo, the request is in the root dir. As such, the following urls would be requested:
/css/foo.css
/js/foo.js
If, however the current request is /foo/, the request is in the dir /foo/. As such, the following urs would be requested:
/foo/css/foo.css
/foo/js/foo.js
Use absolute urls
The way to avoid this is simply to use absolute urls in your link/script tags:
<link rel="stylesheet" type="text/css" href="/css/foo.css">
<script src="/js/foo.js">
That way, irrespective of the current url - the exact url in the link and script tag is used.
Base tag - be careful
You can as you note in your own answer use the base tag - but it's much more obvious to not rely upon it. There are also problems with some browsers e.g. converting "#" into an a request for "/#" which is another reason to ignore the base tag's existence.
Since I do not have enough rep to upvote a proper answer, here it is: the answer by Stormy Weather is a very good thing to follow.
Not only will it serve all files directly, it will also do this for folders. This is very useful if you are starting a new temp project, or need some testing code out quick. Just create a new directory with an index.php in it, and the server will serve it just fine.
Of course, without the -d flag this will work too, but you have to type the entire filename too, which can be quite confusing.
Anyway, I'd have preferred to just upvote the answer above me, but alas.
After searching long enough I found that I could use:
<base href="/beta/" />
in the head section of each HTML file to fix the relative paths. I do not really care to much for this method as it muddies up my markup. If anyone else has ideas please share them with me.
By the way I'm using MAMP Pro on a Macbook Pro.
I don't get it.
When trying to include files from different directories, i'm sure i must be missing something real simple.
Site structure is like this.
if i include("includes/header.php); from inside the /reports/top_sellers_report.php file, the call to the css file doesn't work.
To make it work i must put ../styles/styles.css
But then, if i open "product_dtails.php from the root, it too includes the header, and then the css file won't load and i need to remove the ../ to make it work.
I can't win...
Am i missing something? 4 hours of searching online suggests i am!
Your problem is not really PHP-related. Just look at the URLs:
http://www.example.com/
http://www.example.com/reports/
http://www.example.com/styles/styles.css
Depending on where you are on your site, the relative path to styles.css might change, thus when you access http://www.example.com/reports/ you have to use ../styles/styles.css, wheras in http://www.example.com/ you have to us styles/styles.css.
Probably the easiest way to fix your problem is either by using the absolute URL like http://www.example.com/styles/styles.css or the base-path /styles/styles.css instead of ../styles/styles.css. I recommend the latter.
I think you should look into using an MVC approach as this would probably help you organise your code better:
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
The include statements are evaluated on the server, so wherever you are including the file from, you need to specify it relatively from there. The CSS 'include' is being evaluated in the browser, so you need to make the file path relative from that directory.
I suggested looking to the MVC approach as it helps you to manage your code and separate the different parts of your website into distinct parts which should help you avoid this issue.
I suppose you can refer to you stylesheet absolutely though (ie http://www.mywebsite.com/dir/dir2/stylesheet.css) on each page and that would also work.
as far as I know, include is relative to your index so you have root/index.php
within index.php you should use include("styles/style.css")
within includes/header.php you'll need to add the ../styles to redirect you to root and then to your styles folder and so on
if you open directly product_dtails.php it will act like the index.php, so you need to just use styles/style.php
Not sure how to word the question but I can give you an example.
Which would be more efficient in terms of like speed and overall "doing it correctly" I guess;
http://www.dlolpics.com/images/troll.jpg
or
images/troll.jpg
I usually leave off my domain URL and do images/troll.jpg but is it really a big difference or if a difference at all?
Any information would be awesome, thank you.
Obviously less content in a web page makes it smaller to download, but that is not a good enough reason alone to justify the shorter one.
The main reason to go with a relative URL (the short one), is to make the web site more portable. For example if you rename a directory higher up in the hierarchy or change your domain name you aren't going through the whole site fixing all your URLs.
Bottom line: Do it for ease of maintenance, unless you have thousands of links in a page or you are getting web traffic in numbers like twitter it won't make an appreciable performance difference. Honestly it sounds like you are prematurely optimizing the app, and probably focusing on the wrong things unless you already have profiled your site and noticed performance problems related to the size of the HTML.
There is no difference when it comes to speed, other than a few extra bytes being sent, since the former URL is longer than the latter.
I'd go with the latter form /images/troll.jpg (remember the first /). Otherwise you'll have to modify all your links if one day you decide to move to another domain or you start using HTTPS. A way to overcome this, however, is to use //www.dlolpics.com/images/troll.jpg. This will link to whatever protocol you're currently using; HTTP or HTTPS.
The only downside with /images/troll.jpg I can think of is if the same site is accessible from multiple URLs, for instance if your site can be accessed both at http://dlolpics.com/ and http://www.dlolpis.com/, in which case a user who visits both sites may download the file twice, first from http://dlolpics.com/images/troll.jpg and later from http://www.dlolpis.com/images/troll.jpg. If the full URL approach was used, the browser would realize that it already had the image cached. This, however, should be solved by only allowing access to one of the above URLs (and redirect the other to it), to avoid link confusion and cookie/caching issues.
Here are some different metrics to consider:
Download speed: relative URLs are faster because they are shorter.
URL resolution speed: probably negligible differences that are browser-dependent
Code maintainability: relative URLs are better; if your site ever changes domains or starts using HTTPS, you won't have to update your links.
Based on these, sticking with relative URLs is a better option.
Its not really a matter of efficiency/speed but more about portability. If your media is on the same server as your webpage, a relative url is preferable to an absolute path because if you move the directory or even the site to another domain, that image location will still always be relative to the page it was requested from.
If you are building a site that will get many, many, many hits, you might want to go with a CDN to distribute your media, but if not, either way is about the same in term of speed. Remberber these rules :
Rules of Optimization:
Rule 1: Don’t do it.
Rule 2 (for experts only): Don’t do it yet.
No difference at all other than the few bytes saved in the URL.
Actually, there is a big difference.
http://www.dlolpics.com/images/troll.jpg is an absolute path
while images/troll.jpg is a relative path.
Say you change your website domain from dlolpics.com to whatever.com if you are using absolute paths your images will not load.
In my php script I created a constant variable that defines the base url to be put in my hyperlinks, but do I really need this?
Here is an example of what I mean.
I have this:
// base url is only defined once and reused throughout
define("__BASE_URL","http://localhost/test1/");
print '<a href="'.__BASE_URL.'index.php?var1=open/>Open</a>';
(I have a lot of these spread throughout my script.)
I tried this and it works:
print '<a href="index.php?var1=open/>Open</a>';
So which way is the proper way on doing this? I noticed the second way even works on loading images, css, and javascript files.
It really comes down to how you're structuring your site. Relative URLs are great (by doing href="index.php" you're reallying saying href="./index.php"), but they can start to become messy when you begin spreading pages over multiple directories.
Personally I like to base all of my relative URLs off of the root directory, meaning that all of my URLs start with a slash ('/'). That way it doesn't matter if my script is in / or /admin, as I will always have a constant reference point - the document root - as opposed to some relative directory in the structure.
Your first example, storing document paths in variables, really starts to come in handy when you begin developing larger systems where you want the paths to be configurable. For example, maybe you want your system admins to be able to define where images are pulled from, or where the cached downloads are.
So really consider your use cases and size of your system.
Also keep in mind that if you ever move the script to another server that your URLs and directory structures may change, which could cause havoc (ex., you might have your script moved to a different subdomain, into the document root, etc.). A lot of people will drop in Apache's mod_rewrite in this case.
It depends. Without the __BASE_URL, your link will be relative to the current document. In your case, that means index.php must be in the same directory as the file that has the index.php link on it.
If you have the __BASE_URL, then the link will work no matter where its containing file is located (i.e. doesn't have to be in same directory as index.php).
Another option is to use a starting slash only. Then your link will be relative to your domain root:
print '<a href="/index.php?var1=open/>Open</a>';
In other words, the above link would point to http://localhost/index.php.
It sounds like your question is regarding absolute vs relative URLs. Are you going for portability? It's generally best to use relative URLs, especially if you plan to work in a test environment and then later transfer files to production.