Is it possible to use <base> tag for specific references and URLs only?

2.2k views Asked by At

Good day! Is it possible to use tag for specific references and URL's only? I encountered a problem wherein there is an error anchor tag and its href attribute. For example, there is a module loans, all the PHP files of that module is located at only one folder, take a look of this:

these are the folders and the website:

htdocs - system - members - loans(loan-app.php, loan-sum.php)

If I'm going to use base tag, I got an error whenever I place an src on tags on my code, so as a solution I wont include the value in the base tag in images and only use in links. Is it possible?

Note: Someone suggested me here that I just need to disable the currently opened module to avoid concatenation of url and error. But I realized it just now that I can't because I have another link that is needed to be enable if I open the another BUT they have the same location because they belong at the same module. Unfortunately it can cause concatenation of url.

Anyway, here is my previous question in related to this :

Anchor HREF, if I click again the anchor the link on the address doubles or it concatenate on the existing link. What's the best way to avoid this?

1

There are 1 answers

0
John On

The base element is used for relative URLs. If you use a full URL for an a element, img element, link element, etc the base element will simply be ignored. The base element is extremely useful for when you want to test local versus live (as you should always test locally first). You can not choose for the base element to only apply to certain situations unless you want to get in to extremely convoluted JavaScript which I don't recommend.

Base Element

<base href="https://www.example.com/" />

Relative URLs

<link href="themes/default/style.css" media="screen" rel="stylesheet" type="text/css" />

<img alt="Example" src="images/test.png" />

<a href="loans/">Loans</a>

<a href="loans/sum/">Loan Sum</a>

Use index.php in the folder loans/sum/[index.php] to create clean URLs if you're not using something such as Apache rewrites.

Effective URLs

https://www.example.com/themes/default/style.css

https://www.example.com/images/test.png

https://www.example.com/loans/

https://www.example.com/loans/sums/

Most people start out with convoluting their setups. You should only have the domain name's root most directory (as demonstrated above) and make all your URLs relative.

For the base element itself you should use PHP to determine whether the site is local or live and then echo the appropriate path; that way you don't any convoluted setup like needing two separate copies of the site.

You will want to split your base element's PHP code in to two parts; for the live site the $url2 should only be / and then since you'll naturally have other projects archived and/or in development then for localhost you'll change the path accordingly.

Base Element PHP - Part 1

`<?php echo '<base href="'.$url1.$url2.'" />' . "\n";?>`

Base Element PHP - Part 2

You'll have to do a little bit of work here but this should open your mind up to other juicy possibilities for down the road to make your life easier plus I don't know all your local directory structures. So explore the $_SERVER array to start putting together the $url1 string and remember to keep it simple.

<?php echo '<pre>';print_r($_SERVER);echo '</pre>';?>