14

I asked this question on stackoverflow.com first (question here). In the comments you can read that it would be better if I ask that question here.

On my joomla website I'm using echo $this->baseurl; but it returns the url containing "/index.php". I want to change this to remove "/index.php" from baseurl. But I cannot find the definition line of $this->baseurl;.

I found this thread but this is based on the template of the question owner and in my template there isn't something like this.

Suggestions very appreciated :)

13

$this->baseurl; is basically the shortcut for JUri::base(). It gets the URL you have specified as the live_site variable in your configuration.php file. If this has no value, then it will use some PHP magic based on a few variables.

So, lets run a few tests. Please report the results back:

Test 1:

<?php
  echo 'Rule 1: ' . JUri::base() . '\n';
  echo 'Rule 2: ' . $_SERVER['PHP_SELF'] . '\n';
  echo 'Rule 3: ' . $_SERVER['REQUEST_URI'] . '\n';
  echo 'Rule 4: ' . $_SERVER['SCRIPT_NAME'] . '\n';
?>

Test 2:

<?php
    echo JUri::base();
    echo JUri::base(true);
?>

Test 3:

In the Global Configuration (Joomla backend), try setting URL rewriting to Yes. If this does not work then read Steps 1, 2 and 5 of the Apache section on this page, and try again.


Test 4:

At the top of your template's index.php file, add the following

<?php
    $this->baseurl = JUri::base();
?>
  • Hi, thanks for your answer. I changed the live_site variable in the configuration.php file to my url (nothing changed). Here are the results: Rule 1: my-url.com Rule 2: /index.php Rule 3: / Rule 4: /index.php – Therk Oct 19 '14 at 20:28
  • @Therk - ok, and in your Global Configuration, do you have URL rewriting turned on of off? – Lodder Oct 19 '14 at 20:53
  • @Therk - see test 2 in my answer – Lodder Oct 19 '14 at 20:59
  • you mean mod_rewrite? This is turned off in Joomla configuration. – Therk Oct 19 '14 at 21:52
  • Test 2: echo JUri::base() returns "myurl.com" and echo JUri::base(true) returns nothing – Therk Oct 19 '14 at 21:53
8

The $this->baseurl; is a shortcut to be used from inside a template, instead of calling the JURI:base(); method, of the JURI class. It is part of the core and you can find it at /libraries/joomla/environment/uri.php (~line 218).

If you look at the method, you will see that it checks in the configuration file to see if the $live_url variable is defined. If yes it will return this URL, otherwise it will go through a number of other checks and return your actual base URI.

Most probably you have the live_url variable defined in your configuration.php that includes the index.php at the end. Check there...

Another possibility is that your current template /template system has somewhere defined the $this->baseurl property. You can easily override the $this->baseurl inside your template by re-defining it again at the top of the index.php file of your template, like: $this->baseurl = JURI::base();

*Update:

  • I mentioned where you can find the method, but this does not mean that you should go there and do any modifications to the core. That was only for informational purposes.

Update 2 - for future references

To complete the information given in this post, the JURI::base($pathonly boolean) takes one argument, the $pathonly which is a boolean data type - false is the default.

With $pathonly = true, it will return only the path to the site.

Examples:

<?php echo JURI::base(); ?> Would echo something like: http:://localhost/joomla25/
<?php echo JURI::base(true); ?> Would echo: /joomla25

Note that when $pathonly = true, the trailing slash at the end of the URI path is ommited.

  • Thanks for your answer, I found the method. Should I avoid to change anything there? In the configuration.php file I defined the variable live_site as my url and nothing more (I didn't add index.php somewhere). – Therk Oct 19 '14 at 20:35
  • Yes nothing to change in the core. The only exception is if someone else has already done something there previously... which has caused the function to work this way. Do you really need to define the live_url variable in your config? Usually is not needed. – FFrewin Oct 19 '14 at 20:39
  • Okay :) It doesn't matter if I define live_url variable. It doesn't change anything... The index.php is always coming up. I could fix the problem using code but this would be very hacky and extremely ugly and I don't want to do this. So I want to modify baseurl that the index.php isn't in it. – Therk Oct 19 '14 at 20:41
  • An alternate and quick way is to define your custom baseurl variable inside your template and use this. $mybaseurl = JURI:base(true); echo $mybaseurl; – FFrewin Oct 19 '14 at 20:53
  • And one more thing you could check is if changing your template has any effect on the $this->baseurl. – FFrewin Oct 19 '14 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.