Rewrite rule HTTPS to different port

httpsmod-rewriterewrite

I have an Apache with SSL running on port 444. I need to rewrite all url/requests starts with https://servername ( that is default port 443) to https://servername:444.

I have rule on .htaccess like this:

RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://%{SERVER_NAME}:444%{REQUEST_URI}

This is not working. To check the condition I changed the port on condition as RewriteCond %{SERVER_PORT} =80 and all HTTP requests are forced to HTTPS.

Best Answer

If I understand your situation correctly, your problem is that %{SERVER_PORT} is never equal to 443, since your server is not listening on that port. Thus, your RewriteCond %{SERVER_PORT}=443 condition will never be true.

In fact, I don't believe it is possible to do the rewrite that you intend, unless you put a server listening at port 443 just to handle the initial incoming requests that you then immediately redirect to port 444. But if you do that, you might as well not bother redirecting at all; just handle the HTTPS requests at 443.