# How to handle relative URLs correctly with a reverse proxy in Apache

When using a reverse proxy in Apache, handling relative URLs correctly is crucial to ensure that requests are routed properly and content is displayed as intended. Here are steps to handle relative URLs correctly with a reverse proxy in Apache:

### Enable `mod_proxy` and `mod_proxy_http`:

1. Ensure that the necessary Apache modules are enabled:
    
    ```bash
    sudo a2enmod proxy
    sudo a2enmod proxy_http
    sudo systemctl restart apache2
    
    ```
    

### Configure the Reverse Proxy:

1. Open your Apache configuration file:
    
    ```bash
    sudo nano /etc/apache2/sites-available/your-site.conf
    
    ```
    
2. Set up the reverse proxy configuration, specifying the `ProxyPass` and `ProxyPassReverse` directives:
    
    ```
    <VirtualHost *:80>
        ServerName your-domain.com
    
        ProxyPass /app1/ <http://internal-server:8080/app1/>
        ProxyPassReverse /app1/ <http://internal-server:8080/app1/>
    </VirtualHost>
    
    ```
    
    - `ProxyPass` defines the mapping of the URL path to the backend server.
    - `ProxyPassReverse` ensures that the URLs in the response headers are also rewritten to match the proxy.

### Handle Relative URLs:

1. Use the `ProxyHTMLURLMap` directive to handle rewriting of relative URLs in HTML content:
    
    ```
    <Location /app1/>
        ProxyPass <http://internal-server:8080/app1/>
        ProxyPassReverse <http://internal-server:8080/app1/>
        SetOutputFilter proxy-html
        ProxyHTMLURLMap <http://internal-server:8080/app1/> /app1/
    </Location>
    
    ```
    

### Additional Configuration (If Required):

1. Rewrite Rules: You might need additional `mod_rewrite` rules to adjust URLs if the application requires specific transformations for internal paths.
2. SSL Termination: If your proxy server terminates SSL and the backend server uses HTTP, ensure that URL paths are correctly handled in both secure and insecure contexts.
3. Caching and Compression: Be cautious with caching and compression configurations. They can affect how resources are served and how URLs are handled.

### Testing:

1. After making these changes, restart or reload Apache:
    
    ```bash
    sudo systemctl restart apache2
    
    ```
    
2. Test by accessing the application through the configured URL. Check that pages load correctly, and relative URLs point to the right resources.

By configuring the reverse proxy correctly and handling relative URLs, you ensure that content, including internal links and resources, is correctly mapped and displayed when accessed through the proxy. Adjustments might be necessary based on the specific requirements of the application being proxied.