How do I hide a specified port from an URL?

9,101

Solution 1

The details from this comment suggests that example.com A is pointed to a public IP and the servers are behind a NAT with port forwarding to the two servers:

The problem is that the site using the port 10001 is hosted by another server. The example.com is 172.31.1.10 and the example.com:10001 is 172.31.1.11.

You could have

  • a reverse proxy at your 172.31.1.10:80, proxying queries to 172.31.1.11:10001. As the server is Apache 2.4, the correct documentation is: Apache Module mod_proxy.

  • a new proxy server listening to external example.com:80 with reverse proxies to both 172.31.1.10:80 and 172.31.1.11:10001 (or whatever the internal port of this other server is). Nginx would be an efficient alternative for this. Documentation: NGINX Reverse Proxy.

With both cases the reverse proxy needs to be configured either as a subfolder or a subdomain.

Solution 2

The short answer is no. The browser is what's hiding the port from users; when you use http, it automatically assumes port 80 and when you use https it assumes port 443, hiding these ports from the user. Using any nonstandard port, most browsers would show the port as part of the address.

If I'm understanding your issue properly, there are a few ways to host multiple sites on the same IP, however. Instead of using a different port, you could use a subdomain (myothersite.example.com), or you could use a path below example.com such as example.com/mysite2.

Solution 3

If I understand your question you can accomplish this using the [P] flag of mod_rewrite:

RewriteRule "/(.*)" http://example.com":10001/$1" [P]

There are additional considerations and you should probably use mod_proxy directly.

Share:
9,101
Mrwut
Author by

Mrwut

Updated on September 18, 2022

Comments

  • Mrwut
    Mrwut almost 2 years

    I have one server on example.com, and I have another one on example.com:10001.
    When users try to reach example.com:10001 they end up on example.com:10001/index.php/login and that's how it should be.

    Is there a rewrite rule, to hide the 10001 port so it looks like example.com/index.php/login?

    • Admin
      Admin over 5 years
      You can't 'hide' the port unless the content is reachable on http or https (port 80 or port 443). There's always a port being used, just the defaults of 80 (for http) and 443 (for https) aren't displayed. You can't pretend 10001 isn't 10001. mod_rewrite changes where the browser goes to, not just what the browser displays in the address bar.
  • Mrwut
    Mrwut over 5 years
    You are right I'd like to host multiple sites. The problem is that the site using the port 10001 is hosted by another server. The example.com is 172.31.1.10 and the example.com:10001 is 172.31.1.11
  • Halfgaar
    Halfgaar over 5 years
    @Mrwut in that case you can look into proxying, i.e. sending the requests onward to another server.
  • MrWhite
    MrWhite over 5 years
    Although the OP currently has two different sites on the same hostname/URL (distinguished by the port). So they are going to have to first create a different host/subdomain/subdirectory in order to distinguish requests for the two sites.
  • Swapnil Kumar
    Swapnil Kumar over 5 years
    If the two sites are on the same hostname, but different IP addresses, then that sounds like he's probably already doing some form of proxying.