Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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.
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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.
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
My goodness, the designers are really out of hand at your local if they are complaining about URLs.
Well, one thing maybe put some javascript in your theme, so that for this user the address bar is always hidden.
Mod rewrite is a nice idea though, Moodle's urls are really shorter and prettier than most database driven systems'.
I mean just think how your non-technical person would react to:
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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?
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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.
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
eg http://moodle.org/course/view.php?name=Using Moodle
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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!
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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.
Re: Changing name of 'course' directory AND/OR using mod_rewrite to change URL
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