dongxun7962 2019-07-31 06:55
浏览 154

如何使用mod-rewrite相应地重定向API调用和Web应用程序调用

I'm building an application with php that offers both an API interface and a web app(HTML) Interface. I have both of these in a directory in my document root: /www/sys/api and /www/sys/webapp in each directory I have a router which is supposed to handle routing

/www/sys/api/public/api-index.php

/www/sys/webapp/public/webapp-index.php

Now what I want to do is to use a .htaccess file in my /www/sys/ to route any request in the form of /www/sys/api/smth/smth/smth to /www/sys/api/public/api-index.php?path=smth/smth/smth and any other request (anything other than www/sys/api such as /www/sys/hello or just /www/sys/) to /www/sys/webapp/public/webapp-index.php?path=smth.

Right now I have this:

RewriteEngine On

RewriteRule ^api/(.+)$ api/public/api-index.php?url=$1 [L]

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_URI} !^api/public/api-index.php
RewriteRule ^(.+)$ webapp/public/webapp-index.php?url=$1 [L]

which is working to some extent except that:

  1. going to server/sys/api/ sends me to server/sys/webapp/public/webapp-index.php but if I just add one more layer server/sys/api/hhh it works!

  2. I'm not getting the full url param if I'm redirected to webapp I always get webapp/public/webapp-index.php and in API I always get public/api-inedx.php

to conclude I need to get this .htaccess to redirect /sys/api/ to /sys/api/api-index.php and get the actual url right. Thanks in Advance

  • 写回答

1条回答 默认 最新

  • duanhao7786 2019-07-31 08:51
    关注

    After a lot of tweaking I found a solution just posting it here if someone stumbles upon this question - renamed the api-index.php and webapp-index.php to just index.php now just going to /sys/api/ or /sys/webapp/ doesn't lead to nowhere! - sent the path parameter in a htaccess file in each public directory so this is the structure:

    sys
        api
            public
                 index.php
                 .htaccess [2]
        webapp
            public
                 index.php
                 .htaccess [3]
        .htaccess [1]
    

    [1]:

    RewriteEngine On
    
    RewriteRule ^api/(.*) api/public/$1 [L]
    
    RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
    
    RewriteRule ^(.*)$ webapp/public/$1 [L]
    

    [2] and [3]:

    Options -MultiViews
    
    RewriteEngine On
    
    Options -Indexes
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line