Forum archive
Redirect ip:port to domain:port
Hi everyone. I running Turnkey wordpress on proxmox 6.1-3. I can access webmin, adminer as well as webshell with https://mydomain.com:port
As you know lets encrypt cant config ss; for ip address (only for domain) . Have any way to force redirect all traffic https://ip:port to https://mydomain.com:port ?
I try with htaccess but it's not working
<IfModule mod_rewrite.c>
RewriteEngine on
# Redirect all http or https www.domain.com to https domain.com
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
# Redirect all subdomain (mail.domain) to domain
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
# Redirect ip:port to domain:port
RewriteCond %{SERVER_ADDR} = 123.123.123.123 [NC]
RewriteCond %{SERVER_PORT} = 2000 [NC]
RewriteRule (.*) https://domain.com:2000 [R=301,L]
</IfModule>
Like I say Adminer should be easy. Just adjust the Adminer Apache conf file (/etc/apache2/sites-available/adminer.conf) as per my previous post.
If you want to also redirect Webshell and Webmin from IP to domain name, then you can do that too. I've detailed the steps that should be required below. Please note that I haven't tested this and it's off the top of my head. So I might have made a mistake somewhere. I'll note how to double check your Apache config further down...
First step is to stop & disable stunnel, so it's no longer listening on ports 12320 & 12321. In v14.x & v15.x you can do that like this:
Then make Apache listen on those ports:
Then I'd recommend configuring a separate site file for Webmin & Webshell, let's call it webtools. (Because I've written this as a heredoc, you should be able to copy/paste this whole next code block into your terminal and it should create the file with the contents).
cat > /etc/apache2/sites-available/webtools.conf <<EOF # Webshell reverse proxy and redirect <VirtualHost *:12320> SSLEngine On RewriteEngine On RewriteCond %{HTTP_HOST} !^domain.com$ RewriteRule ^/(.*) https://domain.com:12320/$1 [L,R=301] ProxyRequests Off ProxyPreserveHost On ProxyPass / http://127.0.0.1:12319/ ProxyPassReverse / http://127.0.0.1:12319/ </VirtualHost> # Webmin reverse proxy and redirect <VirtualHost *:12321> SSLEngine On RewriteEngine On RewriteCond %{HTTP_HOST} !^domain.com$ RewriteRule ^/(.*) https://domain.com:12321/$1 [L,R=301] ProxyRequests Off ProxyPreserveHost On ProxyPass / http://127.0.0.1:10000/ ProxyPassReverse / http://127.0.0.1:10000/ </VirtualHost> EOFThen enable your new webtools config, the proxy_http module and restart Apache (to apply the changes, including listening on the new ports):
If Apache fails to start, please share any errors logged. FWIW to double check your config, you can run this:
That should output a clear error message on what has gone wrong.
Important note, this will only redirect HTTPS connections via the IP address to HTTPS connections via the domain. If you try to access the IP (or domain) via vanilla HTTP, then you will still get an error in your browser. Apache is not able to listen with both HTTP & HTTPS on a single port. If you want to do that, then you'll need some sort of multiplexer reverse proxy in front of Apache.
That is outside the scope of what I can help you with here. Although if you give it a go please feel free to let us know how you go. If you're successful then others would likely find it handy. If you get stuck, please feel free to ask. I'm no expert with that, but perhaps I might be able to help with some details?