PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, June 24, 2022

[FIXED] How can I prevent ProxyPreserveHost from redirecting indefinitely?

 June 24, 2022     apache2, mod-rewrite, php, redirect, reverse-proxy     No comments   

Issue

I'm trying to have two subdomains, my.domain.com and foo.my.domain.com, both redirect to the same directory of my Apache2 server (127.0.0.1/my/).

However, I want the scripts there to be able to tell from which domain the request came from. The way I see it, there would be two ways to do that:

  • Using a RewriteRule to add ?foo=bar at the end of every request that contains .php, but that sounds quite dirty
  • Use ProxyPreserveHost to indicate to the PHP script what the original domain is, which sounds more normal and sane.

However, the second I enable ProxyPreserveHost I'm getting a neverending 301 redirection loop. This is the virtual host configuration:

My.Domain.com:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName my.domain.com

    ProxyPass / http://127.0.0.1/my/
    ProxyPassReverse / http://127.0.0.1/my/
    ProxyRequests Off

    SSLCertificateFile /etc/letsencrypt/live/my.domain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/my.domain.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Foo.My.Domain.Com:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName foo.my.domain.com

    ProxyPass / http://127.0.0.1/my/
    ProxyPassReverse / http://127.0.0.1/my/
    ProxyRequests Off
    ProxyPreserveHost On      # creates 301 loop!

    SSLCertificateFile /etc/letsencrypt/live/my.domain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/my.domain.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Note: I've also tried to put ProxyPassReverse / https://foo.my.domain.com/ in the second domain foo.domain.com, but it didn't change anything.

How can I preserve the host without causing a redirect loop?


Solution

Found it:

$_SERVER["HTTP_X_FORWARDED_HOST"] contains the hostname, so foo.my.domain.com in my case. I did not need UseCanonicalName nor ProxyPreserveHost in the end.



Answered By - Rackover
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing