How was a URL like http://stackoverflow.com/posts/

2019-02-26 02:24发布

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

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

But usually, it should be

https://stackoverflow.com/posts/1807491/edit.php

or

https://stackoverflow.com/posts/edit.php?id=1807491

How was

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

I know that Stackoverflow.com was not created by using PHP, but I am wondering how to achieve this in PHP?

8条回答
Root(大扎)
2楼-- · 2019-02-26 03:23

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.

查看更多
一夜七次
3楼-- · 2019-02-26 03:24

Using mod_rewrite this can be achieved very easily.

查看更多
登录 后发表回答