How to change appearance of URL from within a PHP script

前端 未结 2 832
[愿得一人]
[愿得一人] 2020-12-20 05:50

I have some PHP and HTML in the same file, and I am not exactly sure how to make the URL appear as the name of that page.

Here is an example of what I would like to

2条回答
  •  鱼传尺愫
    2020-12-20 06:14

    This is actually a function of your HTTP server software (often Apache), not of PHP.

    What you're seeing happen on sites with "friendly" URLs is that the friendly name is captured in a Regular Expression and then passed to PHP.

    For example:

    GET /HelloWorld
    

    is sent to your web server.. the web server parses it

    RewriteCond ^(A REGULAR EXPRESSION TO CAPTURE YOUR FRIENDLY NAME)$
    RewriteRule page.php?id=(EXPRESSION FROM ABOVE)
    

    In this way your PHP script will always receive the friendly name as a parameter.

    Take a look at "mod_write" for Apache - which you can often create rules for using an ".htaccess" file in the root directory.

提交回复
热议问题