0

我有一个新闻文章网站。在这里,当我点击任何文章时,它的地址看起来像 news.php?id=12 但我希望它看起来像http://paritynews.com/news/YYYY/MM/DD/ <4-digit number> /标题/。在这首先显示新闻类别,然后是文章发布日期,然后是文章编号,最后显示帖子的标题。这里 id 链接http://www.monkks.com/part/parity-newsfinalpage/index.php thnx。

4

2 回答 2

0

在这里,我找到了一篇关于使用自定义 URL 的好帖子。 http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

您可以使用 RewriteRule 生成所需的 URL 类型。

于 2013-07-18T06:05:51.603 回答
0

使用这个 .htaccess 文件和 $_REQUEST 变量。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

这是 index.php 文件。我添加了注释,您必须在其中实际运行查询并处理结果。

//Routing...
$request = substr($_SERVER['REQUEST_URI'],1); //Out of the http://www.domain.com/blog/date -> blog/date is returned
$params  = explode("/", $request); //Creates an array $params = ['blog','date'];

if($params[0] == ""){
//Default Route
    $include_path = "default.html";
}else{
//If there is something in the URL that is useful
//Parse the rest of the url here
switch ($params[0]) {//The first varible Ex: 'blog' or 'news'
    case 'news':
        $article_date = join(array($params[1],$params[2],$params[3]),"-");//Formats the date for MySQL
        $id_number = $params[4];
        //Make sure to escpe the values here
        $sql = "SELECT * FROM table_name WHERE (date_column = {$article_date} AND id = {$id_number}";
        //Run the query with your perfered sql extension, mysqli or pdo
        //Handle errors
        $include_path = "news.php";
    break;
    case 'other':
        $include_path = "something_else.php";
        break;
    }
}
if(isset($include_path)){
    require($include_path);
}
?>
于 2013-07-18T06:06:29.703 回答