Title: WordPress Behind Reverse Proxy?
Last modified: August 19, 2016

---

# WordPress Behind Reverse Proxy?

 *  Resolved [bartmoss](https://wordpress.org/support/users/bartmoss/)
 * (@bartmoss)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/wordpress-behind-reverse-proxy/)
 * Hi Guys,
 * I want to run wordpress behind a reverse proxy. Setup will be:
 * [http://www.example.com:80](http://www.example.com:80) (proxy)
    –> accesses localhost:
   12345 (wordpress).
 * WordPress automatically redirects to localhost:12345 after the initial setup,
   this is easily fixed by editing the url/hostname in the mysql database. However,
   now it will do redirects to itself in an endless loop. I understand why this 
   happens (wordpress will see the localhost:12345 in the http get request and say“
   hey thats not my canonical hostname”); is there an easy way to stop this? I just
   want wordpress to hand out pages, and let me worry about hitting the right hostname,
   I know what I am doing. 😉
 * Any help appreciated.
 * best wishes
    Nils

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [bartmoss](https://wordpress.org/support/users/bartmoss/)
 * (@bartmoss)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/wordpress-behind-reverse-proxy/#post-846705)
 * And ProxyPreserveHost On seems to do the trick.
 *  [lecharabia](https://wordpress.org/support/users/lecharabia/)
 * (@lecharabia)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/wordpress-behind-reverse-proxy/#post-847030)
 * Hi bartmoss,
 * I saw that you resolved your case. I have the same problem and still no resolved.
 * Can you help me ?
 * Thanks
 *  [westerfunk](https://wordpress.org/support/users/westerfunk/)
 * (@westerfunk)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/wordpress-behind-reverse-proxy/#post-847075)
 * DO THIS AT YOUR OWN RISK!
 * BACK UP ALL YOUR FILES BEFORE YOU START MANUALLY EDITING THE CODE!
 * I have a solution that works, but it requires some extra code. It differentiates
   between a web client hitting the web server directly (as in most hosting situations)
   or hitting the web server through a reverse proxy. I wrote this code a year ago
   for another site I own that runs behind an Apache reverse proxy I run at home.
 * I took this code and then added it to a my-hacks.php file I created in the WordPress
   root directory. Then, I went into the WP-Admin and turned on the my-hacks.php
   legacy functionality under Settings, Miscellaneous.
 * Here’s the code:
 *     ```
       <?php
   
       function writeIPAddress() {
       	if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) == '') {
       		return $_SERVER['REMOTE_ADDR'];
       	}
   
       	else {
       		return $_SERVER['HTTP_X_FORWARDED_FOR'];
       	}
       }
   
       ?>
       ```
   
 * Then I took the function “writeIPAddress()” specified above and replaced every
   instance of “$_SERVER[‘REMOTE_ADDR’]” in each PHP file where an IP address is
   either called for or displayed (such as “comment.php” under the wp-includes folder).
   Let me know how this works for you.
    ——————————————–
 * UPDATE:
 * I should refine this a bit further … the only file I had to update was comment.
   php and there was only one place in the code where I replaced the function.
 *  [kgopika](https://wordpress.org/support/users/kgopika/)
 * (@kgopika)
 * [17 years ago](https://wordpress.org/support/topic/wordpress-behind-reverse-proxy/#post-847109)
 * Well we just added
 *     ```
       if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
               $list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
               $_SERVER['REMOTE_ADDR'] = $list[0];
         }
       ```
   
 * to the top of our wp-config.php on [kerala online](http://keralaonline.com)

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘WordPress Behind Reverse Proxy?’ is closed to new replies.

## Tags

 * [domain](https://wordpress.org/support/topic-tag/domain/)
 * [hostname](https://wordpress.org/support/topic-tag/hostname/)
 * [proxy](https://wordpress.org/support/topic-tag/proxy/)
 * [redirect](https://wordpress.org/support/topic-tag/redirect/)
 * [reverse proxy](https://wordpress.org/support/topic-tag/reverse-proxy/)
 * [url](https://wordpress.org/support/topic-tag/url/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 4 participants
 * Last reply from: [kgopika](https://wordpress.org/support/users/kgopika/)
 * Last activity: [17 years ago](https://wordpress.org/support/topic/wordpress-behind-reverse-proxy/#post-847109)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
