How to access query string parameters after “Cleaning” your PHP REST API?

前端 未结 2 633
青春惊慌失措
青春惊慌失措 2021-01-24 12:51

To give some context, I initially started out with my api looking like this:

http://myserver/api.php?action=projects

In my api.php file I have

相关标签:
2条回答
  • 2021-01-24 13:38

    You can use these rules:

    RewriteEngine On
    
    RewriteRule ^(?:action|api\.php)/([^/.]+)/?$ api.php?action=$1 [L,NC,QSA]
    
    0 讨论(0)
  • 2021-01-24 13:41

    Change to this:

    RewriteEngine On
    RewriteRule ^action/([^/\.]+)/?$ api.php?action=$1 [L, QSA]
    

    You need the QSA parameter to keep the querystring intact. So now this is possible:

    /action/projects/?my=name
    

    Or you can do this:

    RewriteEngine On
    RewriteRule ^action/([^/\.]+)/([^/\.]+)/?$ api.php?action=$1&method=$2 [L, QSA]
    RewriteRule ^action/([^/\.]+)/?$ api.php?action=$1 [L, QSA]
    

    Now you will have these urls:

    /action/projects/add/
    /action/projects/
    
    0 讨论(0)
提交回复
热议问题