How to change your URL in Moodle

When you are running a Moodle website, there may be times when you have to change

your URL. For example, you may have sold the website, or maybe you purchased a shorter domain name, and want to replace the old one. In this tutorial we will show you how to change your URL in Moodle. We are going to use the replace script, that is included with Moodle. It essentially searches the database for the old URL, and replaces it with the new one. After changing your URL in Moodle, you may still need to move the files to a new location, based on your setup.

Note: Any time you are modifying a database, we recommend performing a backup first.

Get Started with Moodle on a Dedicated Hosting server today!

Changing your Moodle URL

  1. Login to the Moodle Dashboard.
  2. Add /admin/tool/replace/index.php to the end of the URL, and hit Enter on your keyboard. The address will look like this:
    https://example.com/admin/tool/replace/index.php
    (Be sure to replace example.com with your Moodle site. With older versions of Moodle add /admin/replace.php to the end of the URL.)

    You will then be on the DB search and replace page. It will look like this:
    Find and replace in Moodle

     

    You will also see a message stating “This script is not supported, always make complete backup before proceeding! This operation can not be reverted! Several tables are not updated as part of the text replacement. This include configuration, log, events, and session tables.

  3. In the Search whole database for field, enter your existing URL. in my test I entered:
    https://example.com
  4. In the Replace with this string field, enter the new URL. In my test I entered:
    https://newdomain.example.com
  5. Check the box for Shorten result if necessary. Confirm the change, by checking the box for I understand the risks of this operation, then click the Yes, do it! button.
    Changing URL in Moodle
     
  6. It is finished when you see a message stating “…finished” at the bottom. Click the Continue button to return to the Moodle dashboard.
    URL changed in Moodle with replace.php script
     
  7. Using FTP or File Manager, navigate to where Moodle is installed. Open the config.php file, and locate the line starting with:
    $CFG->wwwroot
  8. Update the line to your new address, in my test I changed from this:
    $CFG->wwwroot = 'https://example.com';

    To this:

    $CFG->wwwroot = 'https://newdomain.example.com';

    Here is a screenshot of the updated line:
    Updating URL in config.php file in Moodle

     
  9. Save your changes and you are done. Now you can move your site (if necessary) to the new folder location.

Congratulations, now you know how to change your URL in Moodle!

JB
John-Paul Briones Content Writer II

John-Paul is an Electronics Engineer that spent most of his career in IT. He has been a Technical Writer for InMotion since 2013.

More Articles by John-Paul

18 thoughts on “How to change your URL in Moodle

  1. Hello all, 

    I’m new to Moodle cloud and I have setup and created user account and working fine,

    I don’t know how to allow them to uploade file in their login and also how other teacher can download, please assist me . thanks, God bless 

    1. Hello.

      Thanks for posting your comment regarding reverting the changes. We recommend using a backup (made prior to making database changes) to restore/revert these kinds of changes.

      I hope this helps!
      Sincerely,
      Carlos D.

    1. Moodle Cloud is a hosting platform for Moodle as opposed to self-hosting, where you can edit core files. I suggest contacting Moodle Cloud support to see if they can help you change your URL.

  2. Hi,

    I have one question, hopefully someone can help me..

    Scenario:
    In my office we have 2 connection wifi..1 for staff & 1 for student.
    After i change the url, from staff wifi & using personal mobile data can access Moodle but from student wifi site cannot access..

    I dont know the issues come from moodle itself @ i need to look into our network configure..Anybody can help me?

    Thanks you so much..

    1. Hello Leen,

      Sorry for the problem with accessing your Moodle site. This type of issue would best be answered in the Moodle support forums. Check out this post. The issue may have to do with security settings in Moodle.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

    1. Hello Said,

      Sorry for the problem in your opening your domain. We would need more information in order to give you assistance. We would need the name of the domain and the exact steps you’re doing that lead to the error.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  3. Thanks for this lesson.I implemented it but I got stucked.

    I cannot open my moodle site again.

    What i guess is missing is,I may need to move one file to another location after changing the subdomin.

    Please how do i do that. A solution is urgently needed.Thank you.

  4. Just wanted to note, in my case I also had to change this line in the config.php file:

    $CFG->dataroot  = ‘example.com’;

    1. Hello Andy,

      Thank you for your question. When you run a query, depending n on your website, it can return a massive list of results.

      The Shorten result if necessary option, makes the list less extensive. For example, if 2,000 lines of code were modified, it would shorten the results. It does not affect the functionality of your query. Unfortunately, I do not know the exact cut-off criteria.

      If you have any further questions, feel free to post them below.
      Thank you,

      -John-Paul

Was this article helpful? Join the conversation!