how to create query string for $_GET without HTML form and URL?

后端 未结 3 1491
时光取名叫无心
时光取名叫无心 2021-01-23 12:23

Imaging we are at home page: www.example.com

we have a hyperlink on this page.

Pizzas

when

相关标签:
3条回答
  • 2021-01-23 12:43

    If you're using Apache you can do this with mod_rewrite. You can learn more about here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

    The example rule would be:

    RewriteEngine On
    RewriteRule ^([^/]*)/([^/]*)$ /?page=$1&cat=$2 [L]
    
    0 讨论(0)
  • 2021-01-23 12:43

    well he means he don't know as to capture the $_GET values of www.example.com/category/Pizzas in php source code.

    this is all done with apache mod rewrite or nginx:

    on your index.php page you can define it:

    <?php
    if (isset($_GET['page']) && isset($_GET['cat'])) {
    $page = $_GET['page']; // category
    $cat = $_GET['cat']; // Pizzas
    // From here you can start using both strings
    }
    ?>
    

    php will get this:

    www.example.com/index.php?page=category&cat=Pizzas
    

    and user will see www.example.com/category/Pizzas on URL (thanks to mod rewrite):

    RewriteEngine on
    RewriteBase /
    RewriteRule ^category/(.*)$ index.php?page=category&cat=$1 [L]
    
    0 讨论(0)
  • 2021-01-23 12:45

    In that repo, take a look in public/.htaccess This is where the magic happens. It's the web server which actually does the job of mapping the url to parameters. The code looks like this:

    RewriteEngine on
    RewriteBase /
    RewriteRule ^category/(.*)$ index.php?page=category&cat=$1 [L]
    

    What that's doing is saying any url that looks like category/ will be rewritten to index.php?page=category&cat=

    So index.php actually handles all those requests, and has the correct GET value - even though the url used is different.

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