代码之家  ›  专栏  ›  技术社区  ›  BackSlash

将url转换为一组GET参数

  •  1
  • BackSlash  · 技术社区  · 13 年前

    我想将带有参数的url转换为路径,即。

    http://www.example.com/?page=1 => http://www.example.com/1
    http://www.example.com/?section=books&category=thriller&page=1 => http://www.example.com/books/thriller/1
    

    然后我希望能够获得所有这些参数,以便在查询中使用它们,以便根据这些参数进行选择,我不知道从哪里开始,我该怎么做?我正在使用这个.htaccess将我的所有请求重定向到index.php

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]
    

    如何将路径“转换”为一组参数,以便在查询中使用它们?

    2 回复  |  直到 13 年前
        1
  •  1
  •   Sammitch    13 年前

    你把它倒过来了。您希望用户将请求作为 http://www.example.com/books/thriller/1 ,然后重写 那个 index.php?qs=$1 供PHP处理。通过这种方式,用户可以看到“友好”的URL,PHP可以看到对其友好的格式。

    因此,您的重写规则将更改为如下内容:

    RewriteRule ^(.*)$ index.php?qs=$1 [QSA,L]
    

    然后您可以通过以下方式访问其内容:

    $myQueryVars = explode('/', $_GET['qs']);
    

    http://www.example.com/books/thriller/1 成为

    array( 'books', 'thriller', '1' )
    
        2
  •  0
  •   YOo Slim    13 年前

    我会做这样的事。。。

    RewriteRule ^([0-9]+)$ index.php?page=$1
    RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9]+)$ index.php?section=$1&category=$2&page=$3