Question
How to set up the following redirects in Plesk:
https://example.com:8443 => https://hostname.com:8443
https://203.0.113.2:8443 => https://hostname.com:8443
where:
-
"example.com" is any domain hosted on the Plesk server and resolved to it.
-
"hostname.com" is Plesk server hostname.
-
"203.0.113.2" is Plesk IP address.
Answer
-
To configure the redirect from the server IP address to the server hostname:
https://203.0.113.2:8443 => https://hostname.com:8443
Perform the following steps
-
Connect to the server via SSH.
-
Create the
/etc/sw-cp-server/conf.d/z-plesk.inc
file:# touch /etc/sw-cp-server/conf.d/z-plesk.inc
-
Open the
/etc/sw-cp-server/conf.d/z-plesk.inc
file via any text editor and add the following lines to it:if ($host ~ '203.0.113.2'){
rewrite ^/(.*)$ https://hostname.com:8443/$1 permanent;
}Note: Replace 203.0.113.2 with the correct Plesk server's IP address, and hostname.com with the real Plesk server's hostname
-
Restart
sw-cp-server
andsw-engine
services to apply changes:# service sw-cp-server restart && service sw-engine restart
Note: Even after the redirect is set up and Plesk is secured by an SSL certificate, an attempt to access the
https://203.0.113.2:8443
in a browser will end up with the error regarding the untrusted certificate. It happens because the redirect is performed after the certificate check.
-
-
To configure the redirect from a domain name to the server hostname:
https://example.com:8443 => https://hostname.com:8443
Perform the following steps
Note: If the domain(s) have HTTP Strict Transport Security (HSTS) enabled, disable it before applying the steps below.
-
Connect to the server via SSH.
-
Create the /etc/sw-cp-server/conf.d/z-plesk.inc file:
# touch /etc/sw-cp-server/conf.d/z-plesk.inc
-
Open the
/etc/sw-cp-server/conf.d/z-plesk.inc
file via any text editor and add the following lines to it:if ($host ~ 'example.com'){
rewrite ^/(.*)$ https://hostname.com:8443/$1 permanent;
}Note: Replace example.com with the correct website that resolves to Plesk server's IP address, and hostname.com with the real Plesk server's hostname
For redirect from all hosted domains names to
https://hostname.com:8443
, add the following directives:if ($host !~ 'hostname.com|127.0.0.1'){
rewrite ^/(.*)$ https://hostname.com:8443/$1 permanent;
} -
Restart sw-cp-server and sw-engine services to apply changes:
# service sw-cp-server restart && service sw-engine restart
Note: Even after the redirect is set up and the website is secured with an SSL certificate, in an attempt to access the
https://example.com:8443
in a browser will end up with the error regarding the untrusted certificate. It happens because the redirect is performed after the certificate check.
-