How to change appearance of URL from within a PHP script

前端 未结 2 819
[愿得一人]
[愿得一人] 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:08

    What you need is to learn more on how .htaccess work.

    Here is a good link that got me started:

    .htaccess tricks and tips

    Update:

    Here is a very common practice in many framework where all requests are sent to index.php, and from there you use php to serve the correct page:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ http://www.site.com/index.php [L,R=301]
    </IfModule>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题