Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 969
Steven Avatar asked Nov 18 '25 21:11

Steven


1 Answers

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.

like image 124
Paul Dixon Avatar answered Nov 20 '25 11:11

Paul Dixon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!