2

I'm currently working on a Drupal site (6.*), which when in production mode will be accessed through some kind of http proxy, which means I will have to rewrite all the links for my custom theme if the $_SERVER['HTTP_X_FORWARDED_SERVER'] variable is set to the domain people will access the site from.

The site has a lot of internal linking, mostly through Views. My thought is that the easiest way to solve this would be to hook into the url() and/or the l() functions and post process the url before returning it if HTTP_X_FORWARDED_SERVER is set.

My problem is that I can't figure out how to hook into these functions, or if it's even possible without touching the core, has anyone had to do this? How did you solve it?

UPDATE: I guess I forgot to mention that the proxy will not be located on root level of the proxy domain, so I need to rewrite all urls (both internal links and paths generated by the system to css files and images etc)

Examples:

proxy.com/path -> site.com/lots/of/dirs

proxy.com/path/node/1 -> site.com/lots/of/dirs/node/1

proxy.com/path/sites/all/themes/mytheme/my.css -> site.com/lots/of/dirs/sites/all/themes/mytheme/my.css

Marco
  • 2,329
  • 1
  • 21
  • 25

2 Answers2

3

I'm not sure if I completely understand what you need, but I think you should have a look at the custom_url_rewrite_inbound() and custom_url_rewrite_outbound() functions.

marcvangend
  • 5,592
  • 4
  • 22
  • 32
  • After looking at it more, this is probably the correct way to do it (never heard of those functions before), and it seems to do the trick, and I'll remember this for future endeavours, thank you :) My hack solution gave me the option to have separate settings files for every url that can access the site (including proxies) which has turned out more useful than I had first planned so I might stick to that for the time being :) – Marco Jan 10 '11 at 14:51
  • 1
    I think that you can still differentiate between different incoming URLs inside custom_url_rewrite_inbound() if you switch on the $_SERVER['SERVER_NAME'] variable. – marcvangend Jan 10 '11 at 15:14
  • true that :) though I also need to set a cookie domain for the proxy, and have a different database connection – Marco Jan 11 '11 at 07:38
1

I ended up having to modify the core slightly, replacing the line below in bootstrap.inc

$server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));

with

$host = $_SERVER['HTTP_X_FORWARDED_HOST'] ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST']; 
$server = explode('.', implode('.', array_reverse(explode(':', rtrim($host, '.')))));

Then I just set up a new site folder with my proxy.com url and changed $base_url and $cookie_domain in the settings.php

Marco
  • 2,329
  • 1
  • 21
  • 25
  • 1
    Changing the core code is not a good idea as you will have problems when you try to update drupal version. – João Guilherme Jan 10 '11 at 13:36
  • 1
    I am well aware of this, but the lifespan of this site is around three months, and I was in a hurry to solve it, so this had to do. I'm still open for a legit solution for the problem though :) – Marco Jan 10 '11 at 13:54