Mobile app version of vmapp.org
Login or Join
Shelley277

: Bypass all rules and redirect internally to another folder/file In Apache config file, there are some rules defines like below: <IfModule mod_rewrite.c> RewriteRule ^(Category)/([^/]+)/(.+)$

@Shelley277

Posted in: #Apache #Configuration #ModRewrite

In Apache config file, there are some rules defines like below:

<IfModule mod_rewrite.c>
RewriteRule ^(Category)/([^/]+)/(.+)$ index.php?module=&id=&action= [B,L,QSA]
RewriteRule ^(Listing)/(.+)$ index.php?module=&id= [B,L,QSA]
RewriteRule ^([^/]+)/(.+)$ index.php?module=&action= [B,L,QSA]
...
</IfModule>


All this address web/foo/bar is internally translated (no redirects) into web/index.php?module=foo&id=bar: foo, bar are fed to index.php as params.

Now I want to add a rule which translates web/api?key1=val1&key2=val2 to web/api/index.php?key1=val1&key2=val2 in a similar manner.

I tried placing RewriteRule ^(api)//?(.+)$ api/index.php? [B,L,QSA] at top of all rules, restarted apache but web/api?foo=bar becomes web/api/?foo=bar (with extra / after api) in browser's address bar and the call is redirected to web/index.php (as it throws error from the file).

How to change the RewriteRule to render web/api/index.php when request is made web/api?..?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Shelley277

1 Comments

Sorted by latest first Latest Oldest Best

 

@Heady270

The problem is that you have a api directory. The Apache code that handles redirects to add slashes to the directory is happening before the rewrite rule. Move your api directory to something else (like apiscripts) and then use the rewrite rule:

RewriteRule ^api$ apiscripts/index.php [L,QSA]


You need the flags on the rewrite rule:


L (last) -- so that other rules don't also get triggered
QSA (query string append) -- so that mod_rewrite passes the parameters through


I would also recommend adding some conditions to your last rewrite rule to ensure that it isn't triggered for any paths that actually represent directories, files, or links:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/(.+)$ index.php?module=&action= [B,L,QSA]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme