Question

I have a local windows server with WAMP and I've installed moodle on this server in www folder.

So you can access moodle from here: 78.93.44.123/moodle

I have a cPanel account with Hostgator.

I want my students to access the moodle website from outside the college by: moodle.mcst.edu.sa not 78.93.44.123/moodle.

i.e. I do not want them to see the IP address. So, I've added an A record in Advanced DNS Zone editor in cPanel as the following:

moodle.mcst.edu.sa => 78.93.44.123

This will redirect the URL moodle.mcst.edu.sa to the server home page (www/index.php). actually, I want to redirect moodle.mcst.edu.sa => 78.93.44.123/moodle

So, I've change the content of www/index.php to be:

<?php
header( 'Localhost: /moodel' );
?>

This works fine. It redirects: moodle.mcst.edu.sa => 78.93.44.123/moodle

But the problem is: The URL changes from: moodle.mcst.edu.sa => 78.93.44.123/moodle

which I do not want. I do not want the users to see the IP in address bar.

How can I prevent URL changing? should I use .htaccess file and how? Any rwong step I've done?

Help me please

Thanks


Note: in www/moodle/config.php I vave:

$CFG->wwwroot = 'http://78.93.44.123/moodle';
Was it helpful?

Solution

I am unsure how moodle determines what url you have typed. You can try the following (but I can't test if it actually works):

Set your config to $CFG->wwwroot = 'http://moodle.mcst.edu.sa';

Put a .htaccess file in your www-root and add the following to it:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/moodle
RewriteRule ^(.*)$ /moodle/$1 [L]

This is a rule for mod_rewrite.

  1. It first checks if the url between the host and before the query string (asdf in example.com/asdf?qwer=1) can be matched by the regex ^(.*)$. It won't probably surprise you that this is true for any url. The entire url is stored as the first capture group (because of the brackets; it has preg-like syntax).
  2. It then checks if all conditions are true. %{REQUEST_URI} is the part of the url we matched in step 1, but with a prefix slash. We check if it doesn't start with /moodle already. This way we prevent the rule from matching itself, which would result in an endless loop.
  3. If step 2 is true, then we rewrite the url to /moodle/$1. $1 is replaced by the first capture group in step 1. The flag [L] stops rewriting for this cycle, and the lack of the [R] flag will make this an internal rewrite (instead of an external redirect).
  4. The next cycle it will try to do all these steps again, but it will now fail on step 2. The internally rewritten url already starts with /moodle and therefore the condition is false.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top