why is moodle storing full urls in the database

why is moodle storing full urls in the database

by Robert Brenstein -
Number of replies: 14
I just had to change the IP address of my server and discovered that the old IP address is kept in the database. I recall reading about this problem in the forums but haven't faced this myself until now.

For example, forum posts that include emoticons have them saved as

<img src="http://141.89.100.242/moodle/pix/s/wideeyes.gif" alt="weitaufgerissene Augen" style="font-family: verdana,arial,helvetica,sans-serif;" />

Makes me wonder why these are not saved with relative URLs. After all, the file structure of moodle is fixed. Saving IP address seems legit only in the log and as lastIp in user table IMHO. Well, in uses like a stamp in quizes is also ok. But storing fully qualified URLs sounds wrong to me since it has to create problems when moodle is moved to another server or moodle server changes IP address or domain name.

Supposedly, export-import of courses fixes these but this is no way to go for server with a hundred courses.
Average of ratings: -
In reply to Robert Brenstein

Re: why is moodle storing full urls in the database

by Michael Penney -
It looks like you put your ip address in your config file on the old server?

For better transportability, use your domain name.

Then "" would be "", and you could change your IP without trouble.
In reply to Michael Penney

Re: why is moodle storing full urls in the database

by Robert Brenstein -
Sure but this is a development web site. When I move it to the production location, not just the IP address but also the domain will change. Moodle should not have any intrinsic dependencies on IP address or domain name IMHO.

Consider also people who change service provider to run moodle. Not likely they can retain their domain.
In reply to Robert Brenstein

Re: why is moodle storing full urls in the database

by Garry Boyer -
There is no guarantee that the Moodle installation is going to be in the same relative location (ie /moodle). Perhaps it could be smart and put the right number of ".." based on what page is linking to the final link.

In reply to Garry Boyer

Re: why is moodle storing full urls in the database

by Robert Brenstein -

I think you are talking about the part above moodle folder. Relative url's within Moodle can be constructed relative to the moodle folder. Its actual name is irrelevant since it never has to be referenced explicitely.

For example, the relative url to the emoticon I gave as example would be

<img src="../../pix/s/wideeyes.gif" alt="weitaufgerissene Augen" style="font-family: verdana,arial,helvetica,sans-serif;" />

since forum's php files are two levels deep in the moodle folder.
In reply to Robert Brenstein

Re: why is moodle storing full urls in the database

by John Papaioannou -
It's probably not as simple as that...

Example: The user profile page can print some of that user's forum posts. The forum posts are retrieved through the forum modules lib.php, and they are usually displayed by some script in /mod/forum/. However, in this context they would be displayed by a script in /user/, and so the "../../" scheme would break the smilies.

I didn't think too much about this, but it seems like a good reason at first glance. smile
In reply to John Papaioannou

Re: why is moodle storing full urls in the database

by Robert Brenstein -
Yes, that would break if they were hard coded as they are now. However, the depth can be tokenized and each module substitute accordingly. /mod/forum/ with "../../" and /user/ with "../" Each module knows its depth after all.

If not that, there must be another way smile
In reply to Robert Brenstein

Re: why is moodle storing full urls in the database

by Samuli Karevaara -
Modules might know their depth, but a bigger problem are common functions that can be called from "anywhere", plus common files like CSS, that shouldn't try to point to "../images/picture.gif" as they can be attached to a page at any level...

The site "http://www.moodledomain.net" might be dropped from the front but I vote for URLs like "/path/from/root.html".
In reply to Samuli Karevaara

Re: why is moodle storing full urls in the database

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
And what if people move a site from http://www.domain.net/moodle to http://www.otherdomain.net/ ? URLs in posts that were like /moodle/course/view.php?id=3 will break.

Also, content often ends up in email, where all URLs need to be complete. How can we consistently convert all relative URLs to absolute URLs?

It was problems like this that caused the original decision to always use full URLs.   We had a big discussion about it.
In reply to Martin Dougiamas

Re: why is moodle storing full urls in the database

by Samuli Karevaara -
In your example case, won't the absolute URLs (with the domain name) break also?

When moving to another domain there is a search and replace of some sort to be done anyways, or is my logic incorrect here?

But I also think that absolute URLs are better than the relative ones. Sometimes, for example, I want to find out which places refer to some page. Grepping for "index.php" is quite meaningless. Ok, in a dynamic environment with concatenated paths and so on it tricky also, but impossible with the relative URLs.
In reply to Robert Brenstein

Re: why is moodle storing full urls in the database

by Samuli Karevaara -
The HTML editor also shows this emoticon icon, and the editor can be pretty much anywhere... So it's a tough cookie though it might look soft on the surface.
In reply to Robert Brenstein

Re: why is moodle storing full urls in the database

by Jodi Stiles -

For various reasons, we run a few different installations of moodle on one server (completely different audiences and missions for each so each is customized differently).  However, we often need to copy courses from one version to another (say http://mysite.edu/moodle1/ to http://mysite.edu/moodle2/).  The complete url gets in the way in this case and requires changing many references.  Is there already an easy way to handle this that I am missing?  Or should I get to work on that search and replace feature?

Thanks,

Jodi

In reply to Jodi Stiles

Re: why is moodle storing full urls in the database

by Eloy Lafuente (stronk7) -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Hi Jodi,

when you copy courses between Moodle sites I suppose that you are using the backup/restore utility, isn't it?

If so, absolute links should be automatically changed without problems (the contents of the $CFG->wwwroot variable are detected and modified). Doesn't it work for you?

Ciao smile
In reply to Eloy Lafuente (stronk7)

Re: why is moodle storing full urls in the database

by Jodi Stiles -

Using the backup and restore does work fine on a course by course basis.  In most cases, however, it has been easier for me to copy a whole database in one shot to a new version - and that requires broader search and replace.  BUT, I shouldn't have to do this again (famous last words), so finding a solution is not critical.  Just wondering if anyone had come up with ways to easily change all url references behind the scenes.

Thanks!

Jodi

In reply to Jodi Stiles

Re: why is moodle storing full urls in the database

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
There is a global database search and replace utility here that I made to do just this.

    http://yoursite/admin/replace.php

Be careful, as you can obviously really break a site with this script.