I have set 2 different port with subdomain on each port. For example: port 8080 -> a.example.com and port 8081 -> b.example.com. But when I try to open a.example.com:8081 the browser loads the b.example.com:8081 web page (but the url not changing to b.example.com:8081) and vice versa. How to redirect to the correct port/subdomain, if we open a.example.com:8081 then it will automatically redirect to a.example.com:8080 or maybe to b.example.com:8081?

I am using Apache 2.4 and my vshost:

<VirtualHost *:8080>
    ServerName a.example.com
    DocumentRoot D:/xampp/htdocs/a

<VirtualHost *:8081>
    ServerName b.example.com
    DocumentRoot D:/xampp/htdocs/b

Since Apache isn't matching a named host on that port, it serves the default one which is the first one read from config files (which is why you'll see config files named like 00_default and such - the numeric part forces a sort order)

What you can do for your use case is specify a named host with the "wrong" name on each port that has any request sending a redirect to the correct combination of name and port.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.