skip to Main Content

im desperately trying to make RewriteEngine to rewrite the following pattern:

https://example.com/api/model/id/
https://example.com/staging/api/model/id/

internally to

https://example.com/index.php/model/id/
https://example.com/staging/index.php/model/id/

I already tried several suggestions from several boards but none of them worked out for me. Ideally the rule should just search for "/api/" and replace it with "/index.php/". I can’t figure out, why it is so hard to make that work, my other rules worked out fine till now…

Here is my last try:

RewriteEngine On
RewriteRule ^(.+)/api/(.+)$ $1/index.php/$2 [R=301,L]
# RewriteRule ^/api/(.*)$ /index.php/$1 [R=301,L,NC]
# RewriteRule ^(.+)/api/(.+)$ http://localhost/dev/someFolder/index.php/$2 [R=301,L]

What am I making wrong? I’m just telling the rule to make ($1)/api/($2) to ($1)/index.php/($2), that shouldn’t be that hard. Ideally the rule also shouldn’t care about whats standing before the "/api/" pattern.

2

Answers


  1. Chosen as BEST ANSWER

    I found a work-around, which seems to work out fine in most use-cases:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) ./index.php/$1 [L]
    

    Found the answer here: htaccess remove index.php from url

    Just keep in mind, that you have to put your files in a subdir named after the string you want to replace "index.php" with (e.g. to replace /index.php/$1 with /api/$1 you have to put all your files into a subdir named api).

    This is just perfectly fine for my use-case.


  2. something like this…

    RewriteEngine On
                    
    RewriteRule ^api/(.*)$ /index.php/$1 [NC]
    
    RewriteRule ^staging/api/(.*)$ /staging/index.php/$1 [NC]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search