Apache mod_rewrite RewriteRule incorrectly adding in port AWS
I have an issue where we are running two systems on the same EC2 instances and using AWS Elastic Load Balancer to send request for one systems to port 81.
So for example we have www.example.com and bookings.example.com where the AWS Elastic Load Balancer sends requests for bookings. though to our EC2 boxes on port 80 and request for www. get sent though on port 81.
The customer connects to www.example.com on port 80 but then gets from the load balancer to the server on port 81.
When we add this rule for example to the .htaccess for the www site we have an issues 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 the port 81 is not pushed back to the customer?
I have come up with this alternative:
RewriteRule ^index.html 开发者_C百科http://%{HTTP_HOST}/ [R=301,L,QSA] !-s
But in this example http is hard coded and I would need to make that variable so it can be https when needed. I also have more then just this index.html rule that redirect back to / I have about 30 rules and feel there should be a one liner to fix this passing port 81 back to the customer.
Your alternative will work and could be extended for ssl support, but it's probably not necessary. Apache follows a specific order when creating self referencing URL's. UseCanonicalName, and UseCanonicalPhysicalPort control how they are created regardless of the module (mod_rewrite, mod_alias, etc).
Without knowing more of your configuration, I would suggest starting with these directives in the appropriate 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]
精彩评论