dua55014 2016-11-17 14:06
浏览 44

删除.php后,用.htaccess重写API URL

I'm making a little REST API in PHP but I'm trying to rewrite URL with .htaccess

I'm trying to rewrite URL like this

localhost/api/object_attributes/1/1

to

localhost/api/object/1/attributes/1

In my .htaccess I already made a condition to remove .php but the second one is not working.

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On

    RewriteRule ^api/ api.php
    RewriteRule ^api/object/1/attributes/1 /object_attributes/1/1
</IfModule>

I searched on the web but didn't find any solution and I get no errors / redirections.

  • 写回答

1条回答 默认 最新

  • duanjue2560 2016-11-17 14:14
    关注

    That second rewrite rule you are working on does not really make sense in my eyes. There is no object to be found under that path in your http server.

    I assume you are looking for one of the following two approaches. Which one is not getting clear from your description, the term "I want to rewrite from ... to ..." is ambiguous.

    RewriteEngine On
    RewriteRule ^/?api/object/(\d+)/attributes/(\d+) /api.php?object=$1&attributes=$2 [END,QSA]
    

    RewriteEngine On
    RewriteRule ^/?api/object_attributes/(\d+)/(\d+) /api.php?object=$1&attributes=$2 [END,QSA]
    

    A general hint: you should always prefer to place such rules inside the real http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server, often for nothing. They are only provided for situations where you do not have control over the host configuration (read: really cheap hosting providers) or for applications that need to write their own rewrite rules in a primitive way (which is an obvious security nightmare).

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集