How was a URL like http://stackoverflow.com/posts/1807421/edit created in PHP?

后端 未结 8 1019
暖寄归人
暖寄归人 2021-01-23 06:10

When you edit a question on stackoverflow.com, you will be redirected to a URL like this:

https://stackoverflow.com/posts/1807421/edit

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-23 07:16

    With apache and PHP, you might perform one of your examples using a mod_rewrite rule in your apache config as follows:

    RewriteEngine On
    RewriteRule ^/posts/(\d+)/edit /posts/edit.php?id=$1
    

    This looks for URLs of the "clean" form, and then rewrites them so that they are internally redirected to a particular PHP script.

    Quite often rules like this are used to route all requests into a common controller script, which might do something like instantiate a "PostsController" class and ask it to handle an edit request. This is a common feature of most PHP application frameworks.

提交回复
热议问题