Search code examples
apacherootcontextpath

Apache - How to get resources from the context path and not default when trying to get from /


I have various web application running on my apache server.

I can access the first web app by using http://localhost:8080/app1/login

But within the login page, I am trying to access an image, login.png as "/images/login.png". This tries to get the images from the default web app and not the web app on which it is currently working from.

I do not want to change the default web app of apache. Is there a way that I can access the image like "/images/login.png" and not as "/app1/images/login.png" or "../images/login.png"


Solution

  • Or if you want to use a RewriteRule add this to your webserver roots .htaccess. This checks if the Requested filename is not a physical file or directory. if it's not. it redirects to your /app directory

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^/images/(.*) /app1/images/$1 [L,QSA]
    </IfModule>