如何将GET参数显示为新目录?

时间:2017-06-05 09:54:32

标签: php server

我目前正在开设一个网站,学生可以创建可以被他人评论的文章

我已经开发了一个脚本,可以创建它们并将它们存储在mysql数据库中。

这些文章中的每一篇都应该在www.mydomain / articles / xyz /

下提供

但我不想为每个人创建一个新目录。那么是否可以传递参数article = xyz (www.maydomain/articles/articles.php&articles=xyz)以显示如前所述?

如果我的问题太复杂,我很抱歉。如果您对此有疑问,请随时与我联系! :)

2 个答案:

答案 0 :(得分:1)

也许您可以使用.htaccess文件执行此类操作 您可以将每个页面重定向到index.php页面

<IfModule mod_rewrite.c>
    RewriteEngine On
    DirectoryIndex index.php
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule . /index.php [L]
</IfModule>

然后您可以在php文件中处理您的域名:

<?php

/*
Example URL
$url = www.maydomain/articles/querystring/articles/xyz/param2/value2/param3/value3
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
*/

$url = "www.maydomain/articles/querystring/articles/xyz/param2/value2/param3/value3";
$url = explode("/querystring/", $url);

/*
Where $url[0] is (www.maydomain/articles),
and $url[1] is the rest of it (queryqtring/articles/xyz/param2/value2/param3/value3)
*/

// If you need you can include your page articles on your index page like this
$page_name = explode("/", $url[0]);
$page_name = end($page_name);
include("/path to your directory/". $page_name .".php");

$query_string = $url[1];
// And one more explode for query string:
$query_params = explode("/", $query_string);


for($i=0; $i<count($query_params); $i++)
{
    // odd value is GET name
    $key = $query_params[$i];
    // even value is GET value
    $value = (isset($query_params[$i+1])) ? $query_params[$i+1] : "";
    $_GET[$key] = $value;
    $i++;
}

echo "<pre>";
print_r($_GET);
echo "</pre>";

/*
// GET Output
Array
(
    [articles] => xyz
    [param2] => value2
    [param3] => value3
)
*/

?>

答案 1 :(得分:0)

可以使用htaccess实现,请参阅此htaccess教程,这将指导您

https://codex.wordpress.org/htaccess

https://www.sitepoint.com/htaccess-for-all/