Changing name of 'course' directory AND/OR using mod_rewrite to change URL

Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Miles Tillinger -
Number of replies: 16
Has anyone changed the 'course' directory name and successfully done the required code changes to allow it all to work?

eg. a url

http://www.someurl.com/course/view.php?id=4

becomes

http://www.someurl.com/somethingelse/view.php?id=4

Before I try myself, is it a simple find & replace? Or does it go deeper than just the code?

I can use mod_rewrite to allow course URL to be accessed this way, but I'd ultimately like to be able to have Moodle's URL also changed.
Average of ratings: -
In reply to Miles Tillinger

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Peter Jeans -
I am not a developer, do not have the time nor technical prowess fro this, but I too would like to be able to change the titles of "course" to something else sitewide i.e. "Resource Area" or somthing else. It would be good if you could specify that somewhere in moodle like you do for the word for teachers and students. Also need more categories of user... like parents! And the ability to create new users with different permissions???
In reply to Miles Tillinger

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers
You would have to make a lot of code changes, and even then, I wouldn't be too comfortable...

I'm curious, why do you want to do this? Is it just so the URL in the address bar looks different, or is there another reason?

mike
In reply to Mike Churchward

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Peter Jeans -
In my case I am developing an online resource area for teachers... rather than a school room so to speak. Calling particular resource areas courses doesnt really fit. Moodle does have wider applications than only course management ... ie content management also.
In reply to Peter Jeans

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers
Absolutely, but is it the URL you are worried about?

You can change the word 'course', used in all of the text of the site very easy. Just edit the language file you use, anc change the definition of 'course' to whatever you want to call it. I did the same thing for one of my sites, changing the word 'course' to 'topic'.

If its the URL you're worried about, that's different. You can probably do something like the post below, in that case.

mike
In reply to Mike Churchward

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Peter Jeans -
Thankyou mike... noi I am not interested in changing the URL... isnot important to me. Thanx again
In reply to Mike Churchward

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Miles Tillinger -
fyi Mike, it is mainly an aesthetics issue raised by non-technical person and I just wanted to know if anyone else had gone to the effort. We use Moodle with more of a 'Groups' and 'Communities' paradigm and even though we've got a great language set for this there's just no pleasing some people!

I guess another possible benefit would be working towards implementing search-engine-friendly URL's site wide either by using clever mod_rewrite directives or by hacking the Moodle core.
In reply to Miles Tillinger

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Michael Penney -

My goodness, the designers are really out of hand at your local if they are complaining about URLssmile.

Well, one thing maybe put some javascript in your theme, so that for this user the address bar is always hiddensmile.

Mod rewrite is a nice idea though, Moodle's urls are really shorter and prettier than most database driven systems'wink.

I mean just think how your non-technical person would react to:

http://blackboard.humboldt.edu/webapps/portal/frameset.jsp?tab=courses&url=%2Fbin%2Fcommon%2Fcourse.pl%3Fcourse_id%3D_2678_1

In reply to Miles Tillinger

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Miles Tillinger -
I still can't find any other posts in reference to setting up short url's for courses.

What I'm thinking of doing is:

- during the Course creation/update process writing directives to .htaccess that generate a redirect to a course home page based on the course's Short Name e.g. www.someurl.com/courseshortname. Short Name would be stripped of spaces or have them replaced with underscores.

Is anything like this planned for future Moodle releases? Has anyone achieved the same thing using a different method?
In reply to Miles Tillinger

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Martín Langhoff -
Miles,

there are several ways of doing it without changing Moodle itself.

The gist of it is catching "short" urls that don't match any existing "real" directory or file in Apache and that match your coursenames, and sending the user a redirect to the moodle url. So the user does end up seeing the full Moodle URL for the rest of the session -- there are ways to avoid this, but they have serious drawbacks.

Before starting, bear in mind that you will want to enforce some strict rules as to what shortnames are valid -- you should modify course/edit.php to enforce that validation -- or you could be in for some surprises.

The first, and most elegant way is to write a custom handler in mod_perl, that kicks in before anything else, and checks whether the URL matches with the moodle database, and redirect accordingly. If it doesn't, Apache will take over and try and serve the URL normally. Drawback: you need a mod_perl guru to help you.

Second strategy, almost elegant, have a cronjob read the moodle database and generate a set of mod_rewrite redirect rules every couple of hours, and restart Apache. Drawback: if anything goes wrong (a character you didn't expect in the shortname breaks the regex syntax) Apache could be left unusable. Benefits: oou can write the cronjob in PHP and it's arguably more efficient than mod_perl in terms of memory and CPU.

Third strategy, register a script with a shortname. Create a script called "go" and tell apache that it's a php script (use a <File> section). The publicize http://www.server.com/go/coursename . Writing the 'go' script is easy ;)

Fourth strategy, use a catchall vhost, DNS wildcard and an index.php script. Publicize http://coursename.server.com/ and have a vhost for *.server.com with a smart index.php that does what you want.

Other strategies -- not really recommended

- Using ErrorDocument 404 to point to a php script. Works but it's nasty. Avoid!
- Use a cronjob to create files with the names of the course in the DocumentRoot.
- Use a cronjob to write .htaccess files that do weird, bad things.

With any of these approaches, it's better to segregate it from your "real" installations, either to a separate vhost or to a subdirectory (like /go/ ) to avoid nasty side-effects with namespace clashes between your courses and your real files and directories.
In reply to Martín Langhoff

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

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
Another little trick that Moodle has always supported but is not widely known is that you can use the course shortnames in the URLs for the course home pages. This can make it easy for external systems to link to courses.

eg http://moodle.org/course/view.php?name=Using Moodle
In reply to Martin Dougiamas

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Penny Leach -
Somehow that fact has managed to escape me up until now!

In that case, it's easy to do with mod_rewrite. Probably keep it away from course, so something more like

moodle.org/go/shortname maps to moodle.org/course/view.php?name=shortname

In reply to Martin Dougiamas

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Slobodan Jovcic -
I noticed this feature just yesterday while browsing through the source. Very nice. smile Additionaly, course can be accessed with an external Course ID number:

http://moodle.org/course/view.php?idnumber=22298321_F2007

However, is it safe to rely on these alternative URL parameters, given that they are not widely known and used? I'd hate to see them dissapear without warning, or ignored during security testing. One can imagine a bug fix that resolves a problem with id parameter, but not with name or idnumber.


Code in view.php that retrieves a course record by short name instead of id seems trivial, but still...

Thanks,
Jovca
In reply to Martín Langhoff

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Miles Tillinger -
Thanks for all the great solutions Martin(s) ;)

I'm leaning towards your third suggestion and wanted to discuss it a bit further. Correct me if I'm wrong, the 'go' script would query the db using the coursename from the last part of the URL: http://www.server.com/go/coursename. Then redirect to http://www.server.com/course/view.php?name=coursename. Does that sound right?

Such a small thing I know but it is going to make a lot of ppl happy!
In reply to Martín Langhoff

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Gareth Morgan -

Hi,

I too am looking at a way of making the URLs of Moodle courses and resources more intuitive and discoverable, if possible, by removing all references to php scripts and arguments so that they more closely resemble the REST approach.


To give some examples: 

We would like to replace http://sitename/course/view.php?id=1234 with something like http://sitename/mathematics/M101 or http://sitename/M101  where  M101 is the short name of the course and mathematics the category.

For category listings, we would like to replace http://sitename/course/category.php?id=2 with something like http://sitename/mathematics
 
And finally, to replace deep links to resources of the form http://sitename/mod/resource/view.php?id=123456 with a form such as  http://sitename/mathematics/M101/Introduction or one that uses an index number (as we may not be safe to assume unique resource names), e.g.  http://sitename/mathematics/M101/1


The way in which we are planning to write this at present is one that, admittedly, Martin does not recommend: to create a custom handler for HTTP 404 errors which Apache will use if a URL is not found.  We might employ segregation so that it does not handle all cases of pages not found.  The 404 handler will integrate with Moodle in the sense of having config.php as an include file, and will access the database using the Moodle database functions to try to identify parts of the URL as category names, course names etc. and lookup their numeric equivalents.

It will use a hierarchy of possible ways of parsing the URL to determine which tables in the database it searches, and if it does not manage to generate a valid moodle URL at the end, will then give out a standard “page not found” message.

We might be persuaded to use a different technical approach, if somebody can suggest one, but there are reasons why we do not want to use mod_perl or a cron job.

Since we are going to try to use Moodle standard functions and coding practices as far as possible, I wondered whether anyone had already developed anything similar to this on their own system and whether they would be interested in sharing their code?


Thanks very much,

Gareth Morgan.

In reply to Gareth Morgan

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Slobodan Jovcic -
Well, here's my solution:

a) use mod_rewrite in Apache to transform course/view.php?name=coursename into more user-friendly course/view/coursename.

b) modify the beginning of course/view.php to check the URL, and if it's using the old view.php?id=XX scheme, to redirect to view/coursename. This will make sure users only see view/shortname URL, and is less intrusive to do than edit all the files that generate links to courses in the old view.php?id=XX format (199 instances as of 1.8.2).

Patches are in the attached archive. .htaccess file goes into course/ folder, and the patch should be applied to course/view.php. Additionaly, the following option has to be set in config.php:
$CFG->friendly_urls_enabled = true;


Performance hit:
- Page accessed via short name executes as usual. Overhead consists of Apache mod_rewrite rule processing, and one extra "if" statement in view.php. Impact of both is trivial.

- Page accessed the old way, via id number runs 4 queries on the database before redirecting to the /view/shortname URL: SET NAMES 'utf8'
SELECT * FROM mdl_config
SELECT * FROM mdl_course WHERE category = '0' LIMIT 1
SELECT shortname FROM mdl_course WHERE id = '2' LIMIT 1

First three queries are executed pretty much on every page in Moodle, so either MySQL or Moodle will cache the results. The last query should be very fast and, if necessary, additional code can be written to enable caching by the web server. Total performance impact seems very low, especially when compared to standard 185 queries needed to render a course page.

Changing few most frequently used course links in Moodle, like the main home page and header breadcrumbs, can alleviate the problem. Analysis of the Apache log can give some insight into link usage.

Hope you find this useful.

Jovca
Average of ratings: Useful (1)
In reply to Miles Tillinger

Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL

by Bernard Boucher -
Hi all,
         check that post.

Bye,

Bernard