0

sometimes we make copies of live sites on another server to test new features. A lot of CMS systems hardcode the URL in the database, so it is not possible to just use another URL. On our local systems, we just use the hosts file to redirect the requests to another IP. This is far to complicated for our customers, so we need a simpler way for them. It is possible to use apache (we use apache as webserver) as reverse proxy, so it redirects dev.somedomain.com to anotherdomain.com. The only problem left is, that there are absolute links to anotherdomain.com in the HTML source. Is there a way to just let apache (or another software) replace all links to http(s)://anotherdomain.com to http(s)://dev.somedomain.com in all pages (+js +css)?

Performance is not an issue, since this will obviously never run at a system that is used by more than a few people.

Josef
  • 381
  • 3
  • 9

3 Answers3

2

Apache does have a module for this - it's called mod_filter.

Jenny D
  • 27,780
  • 21
  • 75
  • 114
2

Thanks for the hint to mod_filter. It seems to work now! I had a problem with the fact, that the site uses both, the url with www. and the one without. My configuration is:

    <VirtualHost *:801>
      ServerName www.dev.domain1.com

      ServerAdmin office@domain2.com

     SetEnvIf X-Forwarded-Proto https HTTPS=on

      FilterProvider gzinflate INFLATE resp=Content-Encoding $gzip
      FilterProvider replace SUBSTITUTE Content-Type $text/
      FilterProvider gzdeflate DEFLATE Content-Type $text/
      FilterChain +gzinflate +replace +gzdeflate
      Substitute "s|domain2.com|dev.domain1.com|n"


     ProxyPass / http://www.domain2.com/
     ProxyPassReverse / http://www.domain2.com/
    # ProxyHTMLEnable On
     ProxyHTMLURLMap http://www.domain2.com/ /



      ErrorLog /var/log/apache2/dev-proxy-error.log

      LogLevel warn

      CustomLog /var/log/apache2/dev-proxy-access.log combined

    </VirtualHost>

And for the domain without www

    <VirtualHost *:801>
      ServerName dev.domain1.com

      ServerAdmin office@domain2.com

     SetEnvIf X-Forwarded-Proto https HTTPS=on

      FilterProvider gzinflate INFLATE resp=Content-Encoding $gzip
      FilterProvider replace SUBSTITUTE Content-Type $text/
      FilterProvider gzdeflate DEFLATE Content-Type $text/
      FilterChain +gzinflate +replace +gzdeflate
      Substitute "s|domain2.com|dev.domain1.com|n"


     ProxyPass / http://domain2.com/
     ProxyPassReverse / http://domain2.com/
    # ProxyHTMLEnable On
     ProxyHTMLURLMap http://domain2.com/ /



      ErrorLog /var/log/apache2/dev-proxy-error.log

      LogLevel warn

      CustomLog /var/log/apache2/def-proxy-access.log combined

    </VirtualHost>

The ProxyHTMLURLMap alone is not enough, because it only replaces the exact same domain. on the domain with www the domain without wouldn't be replaced and vice versa.

I hope this helps someone. With this broad filter I would strongly suggest not using it on a production site, though!

Josef
  • 381
  • 3
  • 9
  • 1
    Kudos for the extensive answer! I didn't have the time to write it up this morning, so I'm very glad that my short hint was of use to you. In two days you'll be able to accept your own anser as the answer to the question, I hope you'll come back and do that as it'll probably be helpful for others in the future. – Jenny D Mar 03 '14 at 11:40
-1

I not realy understand what do you want exectly, and my reputation is too low for add a comment.

It's right, you have this situation:

yourCMS.local
|
|
+-----------> Create Copy of the System = dev.yourCMS.local

On your "copied" Webserver server you have this situation:

User add "dev.yourCMS.local" in the browser and your dev server are redirect the connection to the url yourCMS.local but on the same server (lookup 127.0.0.1)

If my understanding correct, try this solution:

  1. Add the entry in the host file (ubuntu /etc/hosts)

    # <IP-Adress>  <Hostname>
    127.0.0.1   yourCMS.local
    
  2. change the apache site configuration file in /etc/apache2/sites-available/yoursite an add:

    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>
    
    ProxyPass / http://yourCMS.local
    ProxyPassReverse / http://yourCMS.local
    
  3. Restart apache

  4. Try it

It's no tested solution. It could be that not solve your issue, because your added url in the browser is different as configured. I never did that in this direction.

Open for other solutions and comments.

scherard
  • 66
  • 3
  • 1
    It does not solve the issue, since every web page in the testing server will contain links to the production server. – Jenny D Mar 03 '14 at 08:05
  • Not only links but also all the resources like css and js can link to the production server. – Josef Mar 03 '14 at 09:38