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

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

    You use Apache's .htaccess/mod_rewrite, and optionally a PHP file, which is the approach I like to take myself.

    For the .htaccess, something like this:

    RewriteEngine On
    RewriteRule ^(.*)$ index.php
    

    Then in your PHP file, you can do something like this:

    The following should get everything after the first slash.

    $url = $_SERVER['REQUEST_URI'];
    

    You can then use explode to turn it into an array.

    $split = explode('/', $url);
    

    Now you can use the array to determine what to load:

    if ($split[1] == 'home')
    {
    // display homepage
    }
    

    The array is starting from 1 since 0 will usually be empty.

提交回复
热议问题