URL rewrite to another folder and file for specific requests

6,400

First, I'd move your .htaccess file to /www/.htaccess. Apache only "executes" .htaccess files it finds along the path of the request. Requests like /api/... will only "execute" an .htaccess in the docroot (/www) and will not "find" nor "execute" your .htaccess in /www/basic/app/public/.htaccess.

Having done that, for (1), do something like

RewriteRule ^basic/$ basic/public/ [NC,L]

For (2), do something like

RewriteRule ^api/(.*) basic/app/router.php?_REQUEST=$1 [NC,L]
Share:
6,400
Tahir
Author by

Tahir

Updated on September 18, 2022

Comments

  • Tahir
    Tahir over 1 year

    I have following directory structure on my local LAMP:

    • www
      • basic
        • app
          • router.php
        • public
          • index.html
          • .htaccess

    and I can access my index page with localhost/basic/public/ and I am not using any virtual hosts.

    Now I am trying to do two things:

    1. Instead of localhost/basic/public/ URL should look like localhost/basic/ to access homepage.

    2. All requests to /api/ should be redirected to router.php. For example if I make a request like /api/user/login it should go to app/router.php where I can execute specific code according to request for API.

    To achieve this I was trying do something like following in .htaccess file but its not working:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} ^/api/ [NC]
    RewriteRule . ../app/router.php [NC,L]
    

    Also I am not sure if we can use relative paths in RewriteRule.

  • Gem
    Gem over 5 years
    I have some issue in Rest API, if i try -> abc.com/api/rest/products?limit=2 my site return 404 page error, how can i solve this and how can i get products using rest API. @Sathiya Kumar