# Rewrite url in Haproxy

**URL:** https://discourse.haproxy.org/t/rewrite-url-in-haproxy/6432
**Category:** Help!
**Created:** [April 3, 2021, 2:16pm UTC](https://discourse.haproxy.org/t/rewrite-url-in-haproxy/6432 "2021-04-03T14:16:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![akashrp](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@akashrp](https://discourse.haproxy.org/u/akashrp)
#### Post date: [April 3, 2021, 2:16pm UTC](https://discourse.haproxy.org/t/rewrite-url-in-haproxy/6432/1 "2021-04-03T14:16:30Z")

</div>

I am using Haproxy Fastcgi to serve php files for wordpress. Static files are served by Nginx. Everything is working but the wordpress permlinks are not working. Here is my Haproxy config file

global  
log /dev/log local0  
user haproxy  
group www-data

```
    # Default SSL material locations

```

defaults  
log global  
mode http  
option httplog  
option dontlognull  
timeout connect 5000  
timeout client 50000  
timeout server 50000  
errorfile 400 /etc/haproxy/errors/400.http  
errorfile 403 /etc/haproxy/errors/403.http  
errorfile 408 /etc/haproxy/errors/408.http  
errorfile 500 /etc/haproxy/errors/500.http  
errorfile 502 /etc/haproxy/errors/502.http  
errorfile 503 /etc/haproxy/errors/503.http  
errorfile 504 /etc/haproxy/errors/504.http

frontend myproxy  
bind :80  
acl url\_static path\_end .gif .png .jpg .css .js  
use\_backend static if url\_static  
use\_backend phpservers  
default\_backend phpservers

backend phpservers  
use-fcgi-app php-fpm  
server server1 /run/php/myapp.sock proto fcgi

backend static  
server server2 /var/run/nginx.sock

fcgi-app php-fpm  
log-stderr global  
docroot /var/www/myapp  
index index.php

Now wordpress works only when the url includes index.php. Without index.php the server returns 404 error.

I want to rewrite the url from

```auto
http://example.com/index.php/hello-world/

```

to

```auto
http://example.me/hello-world/

```

In nginx we use the below config to work

```auto
location / {
                
                try_files $uri $uri/ /index.php?$args;
        }

```

Is their any solution like this in Haproxy

---

<div class="post-metadata">

### Author: ![nebulon42](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nebulon42](https://discourse.haproxy.org/u/nebulon42)
#### Post date: [November 24, 2021, 6:41pm UTC](https://discourse.haproxy.org/t/rewrite-url-in-haproxy/6432/2 "2021-11-24T18:41:05Z")

</div>

It took me some time to find out, but I was successful with

- setting the script name to index.php in the fcgi app with `set-param SCRIPT_NAME /index.php` unless the request did already contain a PHP file
- since the admin backend has its own index.php in `/wp-admin/index.php` there needs to be a separate rule that sets the script name to that when we are accessing an admin URL unless the request did already contain a PHP file
- doing nothing with the path in the frontend/backend

Full fcgi app:

```auto
fcgi-app wordpress
  docroot /var/www/html
  log-stderr global
  acl is_php path -i -m end .php
  acl is_admin path -i -m beg /wp-admin
  set-param SCRIPT_NAME /index.php unless is_php is_admin
  set-param SCRIPT_NAME /wp-admin/index.php if is_admin !is_php
  set-param HTTPS on

```

I was not successful with

- using `index`. As the documentation states this _appends_ the name after the path, so this won’t work
- using `path-info`, this did nothing for me
- doing something with `http-request set-path ...` as this changes the `REQUEST_URI`

What really helped me in finding the solution was adding a line in `wp-config.php` that prints out `$_SERVER` for every request. There you then see what values `SCRIPT_NAME`, `REQUEST_URI` and others have.

E.g. if you are running php-fpm in a container that logs to stderr you could add

```auto
$stderr = fopen( 'php://stderr', 'w' );
fwrite($stderr, print_r($_SERVER, true) . "\n");
fclose($stderr);

```

---

<div class="post-metadata">

### Author: ![nebulon42](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nebulon42](https://discourse.haproxy.org/u/nebulon42)
#### Post date: [November 25, 2021, 8:23pm UTC](https://discourse.haproxy.org/t/rewrite-url-in-haproxy/6432/3 "2021-11-25T20:23:25Z")

</div>

Cannot edit the above anymore. 🙄  
Just to note that I had problems with the login and to solve that one should change the index.php set-param to `set-param SCRIPT_NAME /index.php if !is_php !is_admin`. Not 100% sure why the “unless” version has problems and the “if” version works, but that is what I saw.
