Apache mod_rewrite RewriteRule incorrectly adds to AWS port

I have a problem where we run two systems on the same EC2 instances and using AWS Elastic Load Balancer send a request for one system on port 81.

So, for example, we have www.example.com and bookings.example.com, where the AWS load balancer sends booking requests. although our EC2 boxes on port 80 and request for www. sent to port 81.

The client connects to www.example.com on port 80 and then goes from the server load balancer to port 81.

When we add this rule, for example, in .htaccess for the www site, we have problems with port 81 appearing.

RewriteRule ^index.html / [R=301,L,QSA] !-s
#Results in customer being sent to http://www.example.com:81/


How can I make sure port 81 is not being returned to the client?

I came up with this alternative:

RewriteRule ^index.html http://%{HTTP_HOST}/ [R=301,L,QSA] !-s


But in this example http is hardcoded and I will need to make this variable so it can be https when needed. I also have more than this index.html rule that redirects back to / I have about 30 rules and feel like there should be one liner to fix this forwarding port 81 back to the client.


source to share

1 answer

Your alternative will work and can be extended to support ssl, but it probably isn't necessary. Apache follows a specific order when creating self-bind URLs. UseCanonicalName and UseCanonicalPhysicalPort control how they are created regardless of the module (mod_rewrite, mod_alias, etc.).

Without knowing any more about your configuration, I suggest starting with these directives in the respective VirtualHost.

UseCanonicalName On
ServerName www.example.com:80


EDIT . If you don't want to do this for whatever reason, here's how you fix your rewrite to support ssl.

RewriteCond %{HTTPS} =on
RewriteRule - - [env=req_scheme=http,S=1]
RewriteRule - - [env=req_scheme=https]

RewriteRule ^index.html %{ENV:req_scheme}://%{HTTP_HOST}/ [R=301,L,QSA]




All Articles