Using Clean URLs in RESTful API

前端 未结 3 955
失恋的感觉
失恋的感觉 2020-12-02 11:13

\"Clean URLs\" also known as \"RESTful URLs\" are user-friendly, purely structural, and do not contain a query string. Instead they contain only the path of the resource.

3条回答
  •  遥遥无期
    2020-12-02 11:55

    This worked for me: Put this in the htaccess file at the root of your website.

    
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule api/(.*)$ api/api.php?request=$1 [QSA,NC,L]
    RewriteRule (recipe/.*) api/app.php?request=$1 [QSA,NC,L]
    
    

    And then if you go to the page http://localhost/api/person/susan You will see that it takes you to the file at http://localhost/api/api.php I also have a recipe page that I go to using http://localhost/recipe/edit/2 Put this in the api.php file:

    The variables above will hold the category: recipe, action: add or edit, and the data which can be a number which is the id for the recipe or what ever you want it to be. Then inside the add_recipe.php use the variables to determine whether you are editing or adding a recipe. And if you use the api you can include different files depending on what ajax request you are using to talk to your api.

提交回复
热议问题