apachemod-rewriteurl-rewritingapache2mod-proxy

Combination of URL rewrite and proxying in apache failing - with 404 Not Found Path


Hello Apache experts ,

I have a third party http web application listening on a server on port 8080. The third party application needs request URL to be of the form

http://hostname:8080/?accnum=<account number>

However owing to a legacy integration problem the requester web client makes the request in the form

https://hostname/oldcontext/?acc=&quot;<accountnumber>&quot

Here "oldcontext" is a fixed string and accountnumber is a variable number

For achieving integratiion between requestor and the third party app - I installed an apache server and use apache proxying and apache mod rewrite rewriterule directives to convert the URL format and send it on port 8080

My apache web server configuration looks as follows

<VirtualHost *:80>
    RewriteEngine on
    RewriteCond %{QUERY_STRING} acc=([^&]+)
    RewriteRule ^oldcontext/$ /?accnum=%1 [L,R=301]
    RequestHeader unset Origin

    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html  
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I offcourse have enabled the necessary apache modules as follows and restarted apache

a2enmod rewrite
a2enmod headers
a2enmod proxy
a2enmod proxy_http

With that done when I make a request to URL

http://<my hostname>/oldcontext/?acc=&quot;<account number>&quot;

expecting that to translate and redirect to

http://127.0.0.1:8080/?accnum=<account number>

However

I get following response

{"status":404,"error":"Not Found","path":"/oldcontext/"}

I checked that the proxying is working fine -

my request to

http://127.0.0.1/?accnum=<account number>

get correctly redirected to

http://127.0.0.1:8080/?accnum=<account number>

And I get expected response

The rewrite is failing though as described

And even worse if I include a catch all config fr URL rewrite as follows it still gives me same error

enter code here
<VirtualHost *:80>
    RewriteEngine on
    RewriteCond %{QUERY_STRING} acc=([^&]+)
    # RewriteRule ^oldcontext/$ /?accnum=%1 [L,R=301]
    RewriteRule ^(.*)$  /?accnum=%1  [L,R=301]
    RequestHeader unset Origin

    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html  
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

What am I missing ?

Thank you for your help

Yogesh


Solution

  • Following configuration worked for me

    From what I gather I was basically stuck getting a correct RewriteCond that would match my URL and thus ensure my rewrite rule is fired. Once I figured that I sought help separately just for getting a right rewrite condition ( see my other post Apache mod rewrite RewriteCond regex help needed )

    Since my rewritecond was NOT matching the redirect was sending teh request with "oldcontext" to the new location where "oldcontext" did not exist hence the 404 error

    Once I got the pattern match for my request URL right in my RewriteCond that ensured my rewrite rule was fired . My rewriterule too was not matching and I made change as suggested by https://stackoverflow.com/users/98959/covener and it changed the URL to the new path and then everything worked well

    I am including full config for sake of completeness

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        # LogLevel alert rewrite:trace7
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        ProxyPass / http://127.0.0.1:8080/
        ProxyPassReverse / http://127.0.0.1:8080/
    
        RewriteEngine on
    
        RewriteCond %{QUERY_STRING}  ^acc=[a-z&;]+([0-9]+)[a-z&;]+ [NC]
     
        RewriteRule ^/oldcontext/$ http://%{HTTP_HOST}:8080/?accnum=%1 [L,R=301,B,NE]
    </VirtualHost>