你如何编写动态重定向的htaccess / web.config文件

I understand that this question has been answered many times before, but I haven't seen it answered in a way in which is general enough of an explanation to understand how it works.

I wish to create a redirect with a htaccess / web.config file (not sure which is the proper one to use) which redirects a dynamic url into something which looks like an actual page/folder structure.

The URL typed in for example, www.example.com/index.php?id=1 (or simply .com?id=1) should work alongside a MySQL database in order to work out what the URL of page id 1 should look like, so for example if row ID 1 of the database said the url should be "first_page" the url should redirect to www.example.com/first_page/ and have all of the content of www.example.com/index.php?id=1

Can anyone explain in a general sense how this works? I understand it is simply down to me not understanding- but that's why I'm hoping someone can explain effectively.

Edits:
Working on an Apache server, but I would like to know for both cases as I need to support both with the project I'm working on

I had the same issue and I wanted to create a dynamic redirect to enable pretty links/friendly URLS/SEO URL. I had in mind that the user would click a link and then php would redirect this to a nice looking URL. My assumption was wrong.

You can always change the url that is displayed in the browser via

window.history.pushState

but this only solves 1 part of the problem. The website url look nice but is otherwise useless for search engines and if the user would use this url in future he would not find the same page.

It took me a while to understand that the rewrite url actually does not rewrite the existing url to match the browser url but instead to rewrite the browser url that the user typed in into an url that matches your file structure.

Therefore if a user is looking for

https://www.example.com/pageid/23/joe

the rewrite module would convert this into

https://www.example.com/page.php?pageid=23&user=joe

This can then be used to get the parameters and create the web content dynamically.

Before I started my parameters contained the userid like below(Before) which wouldn't help me as I wanted to see something like (After)
Before:

https://www.example.com/page.php?userid=123

After:

https://www.example.com/user/Joe

With the standard rewrite rule this means also you need to change the logic of the parameters by either adding the username as a parameter or by changing the userid to unique usernames (as substitute for userid).

In web.config (IIS) this would look like:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="rule 1T" stopProcessing="true">
                    <match url="^pageid/([^/]*)/([^/]*)$"  />
                    <action type="Rewrite" url="//page.php?pageid={R:1}&amp;user={R:2}"  />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

The 2nd step that then needs to happen for all your internal links: You need to change they way you create the links. One way could be to create a global function like below that you can wrap around your existing links and that would then translate every link into the pretty link.

function L($search) { //Link Rewrite function
        global $LinkArray; //Use global variable with all Links
        foreach ($LinkArrayas $key => $val) {
           if ($val['OrgLink'] === $search) {
               return $val['PrettyLink'];
           }
        }
        return null;            
}

I hope this helps even though the post is a bit older.