Changing Moodle site address: Too many redirects

Changing Moodle site address: Too many redirects

by Andrey Kladiti -
Number of replies: 3

Hi guys,

Could anyone help me with my problem?

We have published our Moodle site to the external Internet using Azure AD Application Proxy. Alongside this we have changed Moodle site address (say, from "https://oldsite.com" to "https://newsite.com") and set up DNS servers to resolve new name into external and internal addresses for Internet and Intranet visitors respectively (this works correctly). It had been done by our colleagues since I don't have an access to those services.

What have I done with Moodle:
1. Changed $CFG->wwwroot in config.php
2. Run search and replace on "admin/tool/replace/index.php" page to change old address to a new one
3. Purged all caches
4. Installed new certificates for "*.newsite.com"

What have we got:

When I try to access the page from the external Internet, I receive such an activity:

Request URLhttps://newsite.com
Remote Address: <External address>
Status Code: 301 Moved Permanently Location: https://newsite.com/
Request URL: https://newsite.com/
Status Code: 303 See other Location: https://newsite.com
Request URL: https://newsite.com
Status Code: 301 Moved Permanently Location: https://newsite.com/
Request URL: https://newsite.com/
Status Code: 303 See other
Location: https://newsite.com

And this cycles infinitely until browser gives me "Too many redirects" error.
When I try to access the same page from the internal network, I get

Request URL: https://newsite.com
Remote Address: <Internal address>
Status Code: 301 Moved Permanently
Location: https://newsite.com/
Request URL: https://newsite.com/
Status Code: 200 OK ... And then everything goes right.
I appreciate any help with this since publishing our site to the Internet is the main goal of all this activity, and this very feature doesn't work.
Thanks in advance!

P.S. We run Moodle under the following configuration (yeah, I know what it looks like, but this is my heritage):
Windows Server 2012R2 + IIS 8.5 (two servers in NLB cluster)
MS SQL Server 2012

P.P.S. If I change $CFG->wwwroot in config.php back to old value and try to access website from external Internet, I get the following:

Request URL: https://newsite.com
Remote Address: <External address>
Status Code: 301 Moved Permanently Location: https://newsite.com/
Location: https://newsite.com/
Request URL: https://newsite.com/
Status Code: 303 See other Location: https://newsite.com/index.php Request URL: https://newsite.com/index.php Status Code: 200 OK ... And then everything goes right.
...but of course, all links on this page lead to old-style addresses "https://oldsite.com/..."

Average of ratings: -
In reply to Andrey Kladiti

Re: Changing Moodle site address: Too many redirects

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well... something is causing those redirects wink Moodle doesn't know if the source of the request is "internal" or "external" so it's extremely unlikely it's a Moodle issue.

What's the difference between your internal and external network? There's some vital information missing from the above... I'm not quite sure what it is. But the problem lies in the details of your configuration.

In particular, can you confirm that config.php says

$CFG->wwwroot = "https://newsite.com/";  (nothing else, no code)

Can you also confirm (as it common) that your Moodle server effectively has two different IP addresses (internal NAT and external "real" IP) and your DNS setup simply resolved "https://newsite.com/" to the appropriate IP. 

You might also want to check your web server setup for rewrites and redirects as, again, I wouldn't be surprised if you'd put something in there to redirect from the old hostname. It's dead easy to get these rules wrong. 
Average of ratings: Useful (1)
In reply to Howard Miller

Re: Changing Moodle site address: Too many redirects

by Andrey Kladiti -
Hi Howard, thanks for your reply.

Looks like I'm on my way to solve the problem.
At this very moment I implemented a temporary workaround which looks (kinda weird) like this:

if (array_key_exists("HTTP_X_MS_PROXY", $_SERVER))
{
	$CFG->reverseproxy = true;
}
else
{
	$CFG->reverseproxy = false;
}
This is how I can tell internal visitors from external (I beleive the "HTTP_X_MS_PROXY" header is added by Azure App Proxy).
But as far as I found out, the guy who published the website on the Internet has specified the old internal name, because visiting the site from the external network I see these items in the $_SERVER array:

[SERVER_NAME] => oldsite.com  
[HTTP_HOST] => oldsite.com
And there are correct headers when I visit this page from internal network. I suspect this can cause those redirects. I have informed my colleague about it and now I'm waiting for him to change the configuration.
In reply to Andrey Kladiti

Re: Changing Moodle site address: Too many redirects

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Yep - what you CANNOT do is have more that one URL for Moodle. Bad things happen. $CFG->wwwroot needs to be fixed and needs to be used everywhere. You need to work your system configuration around Moodle on this one.