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

后端 未结 8 905
暖寄归人
暖寄归人 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:13

    Using mod_rewrite this can be achieved very easily.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题