如何使用.htaccess PHP仅捕获查询字符串

时间:2019-02-17 06:54:56

标签: php .htaccess

  

.htaccess文件

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /cms_project
 RewriteCond %{REQUEST_URI} single(.*)
 RewriteRule  ^(.+)$ single.php?url=$1
</IfModule>
  

URL:localhost / cms_project

//want to query strings single/posts/1/post-title
echo $_GET['url'];

OR

//function to get url query string convert it to an array
function getUrl() {
    if(isset($_GET['url'])) {
        $url = rtrim($_GET['url'],'/');
        $url = filter_var($url, FILTER_SANITIZE_URL);
        $url = explode('/', $url);
        return $url;
    }
  }
  print_r($_GET['url']);
  

现在,当我输入url时,我想获取url查询字符串    localhost / cms_project / single / posts / 1 / post-title

     

现在我希望获得 single / posts / 1 / post-title ['single','posts','1','post-title'] 数组

     

但是上面的代码仅返回 ['single.php'] 数组

1 个答案:

答案 0 :(得分:0)

尝试一下:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /cms_project

 RewriteCond %{THE_REQUEST} ^GET\ /single\.php\?url=(.*) [NC]
 RewriteRule ^single\.php$ single/%1 [L]
 RewriteRule ^(.*)/(.*)$ single.php?url=$1/$2 [NC,L]  #OR ^single/(.*)$ single.php?url=single/$1
</IfModule>