Remove "controlpanel" redirection in CPanel

A new problem arose when CPanel was upgraded. Someone at CPanel decided it was not enough to have one redirection to their login, at www.example.com/cpanel, and decided to create another one at www.example.com/controlpanel. This was a problem because one of our clients has an application with its own controlpanel already in that location.

Since the redirection was defined as a ScriptAliasMatch in the root of httpd.conf it was not possible to override it using a .htaccess file. To compound the problem this client had a test version of the web site on a sub domain which meant the SCriptAliasMatch could not be overwritten using a vhost include. The offending line could be easily removed from httpd.conf but every time CPanel was updated it would appear again.

To solve this dilema we had to use a little know script hook that CPanel triggers after an upgrade. Using a BASH script we could automatically find the problem line, delete it, and save a working configuration. The BASH script looks like this:

#!/bin/bash
echo "Removing controlpanel redirect"

# Begin search and delete - the following is all on one line

sed -i'.bak' -e '/^ScriptAliasMatch \^\/?controlpanel\/?\$ \/usr\/local\/cpanel\/cgi-sys\/redirect.cgi$/d' /usr/local/apache/conf/httpd.conf

# End search and delete

/usr/local/cpanel/bin/apache_conf_distiller --update
/usr/local/cpanel/bin/build_apache_conf

It was saved as: /scripts/postupcp
And made executable using: chmod +x /scripts/postupcp

Breakdown the BASH code

#!/bin/bash

The first line indicates that the file should be treated as a bash script. When the file is marked as executable this will tell the system how it should treat the file.

echo "Removing controlpanel redirect"

The second line simply writes some text on the screen so that when an update is triggered we can see that this script was triggered.

sed -i'.bak' -e '/pattern/d' filename

The third line does most of the magic using the sed command. This one is very complex so we will break it down some more.

Save and Backup: -i'.bak'

This tells sed to save the changed version using the original filename and create a backup copy of the original with a '.bak' extension.

Match Pattern and Delete: -e '/pattern/d'

'-e' forces sed to look for text using a regular expression. '/pattern/' is the regular expression and 'd' tacked on the end tells sed to delete any matches it finds. We had to use a regular expression because the original string we were searching for contained many characters that have special meaning to sed. Our original text was:

ScriptAliasMatch ^/?controlpanel/?$ /usr/local/cpanel/cgi-sys/redirect.cgi

This string contains the following special characters that had to be escaped: ^ / ? $
We use the \ character to escape special characters so our search pattern ends up being:

/ScriptAliasMatch \^\/?controlpanel\/?\$ \/usr\/local\/cpanel\/cgi-sys\/redirect.cgi/

To make sure that we only delete one complete line of code we add '^' to the front which indicates the beginning of the line and '$' to the end to indicate the end of the line. Our final pattern ends up being:

/^ScriptAliasMatch \^\/?controlpanel\/?\$ \/usr\/local\/cpanel\/cgi-sys\/redirect.cgi$/

filename

In our case the offending line was in /usr/local/apache/conf/httpd.conf. This is the Apache web server configuration file that is generated by CPanel after the upgrade.

apache_conf_distiller --update

Anytime changes are made to http.conf Cpanel must be informed so that it can update its local data store. This is needed so that rebuilds or configuration changes can be applied. The fourth line executes the CPanel distiller.

build_apache_conf

Just to be safe we run the CPanel rebuild script to make sure that we have a working httpd.conf file. It will scan through the updated data store and rebuild the file making sure that all the syntax and commands are correct.

Add your comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  .dEE9Hb.   5@HE@SSM9    .d9@S9b.    dEE@     .d@HSMb.  
dMMP YE8b H5S d55P YS@b d9MEM d9HP Y8Hb
Y8Eb. d9@P MS5 555 9EE @89 8E5
"Y5@9@H" 99M8H5Hb. HS5dMEHb. H8H YM8b. dS99
.dSP""Y8b. "YHMb S5HP "YH@b EH5 "YE5HP955
98M 8ES 8SS 5M8 S9S S@M SSE
YMEb d59P Y@@b d85P YMSb dH8P 89E Y5Sb d85P
"Y@SMSP" "YE89@P" "YH558P" @H@EM8H "Y5H9SP"