dongnanman9093
2017-07-29 13:18
浏览 39
已采纳

如何为单个页面创建多个URL别名?

I'm using PHP and I've read about how to create aliases and do a rewrite rule in Apache.

The thing is that I would rather like a functionality similar to the one that I do have in a Drupal site.

For example:

The URL example.com/info.php?id=18&type=article should be accessible as:

case 1: example.com/article/18

and

case 2: example.com/special-url-to-the-article

I do understand the basics on how to create a specific alias for each article (case 2):

I think that I should check the url with something like $_GET['id'] and $_GET['type'] to look into it in a database that holds the alias and the other values, and if the alias exist form a new url with the id and type and send the user there.

But how do I manage case 1, where I set the type of content and the content id programmatically?

I just need some guidance that could point me out to some direction.

图片转代码服务由CSDN问答提供 功能建议

我正在使用PHP,而且我已经读过如何创建别名并执行重写规则在 Apache

问题是我更喜欢类似于Drupal网站中的功能。

例如:

URL example.com/info.php?id=18&type=article 应该可以访问:

案例1: example.com/article/18

\ n

案例2: example.com/special-url-to-the-article

我了解基础知识 关于如何为每篇文章创建一个特定的别名(案例2):

我认为我应该用som检查网址 类似 $ _ GET ['id'] $ _ GET ['type'] ,在包含别名和其他值的数据库中查看它,如果是 别名存在于具有id和类型的新url并将用户发送到那里。

但是如何管理案例1,我在其中设置内容的类型和内容 id 以编程方式?

我只需要一些可以指向某个方向的指导。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douniao7308 2017-07-29 14:06
    已采纳

    I use a similar rewrite and, right or wrong, this is basically what I do. In both cases, your $_SERVER array should have one or more of these values:

    [REQUEST_URI] => /article/18/
    [QUERY_STRING] => article/18/
    [REDIRECT_URL] => /article/18/
    [REDIRECT_QUERY_STRING] => article/18/
    [SCRIPT_URL] => /article/18/
    [REDIRECT_SCRIPT_URL] => /article/18/
    

    For me, I would have the path of deliberate pages (like an "about us" page or whatever) saved in a database in order to be able to look it up using the path, but if not there, as in this case, then you deal with the path further:

    # Assign one of the server paths that is most reliable
    $path  = $_SERVER['REDIRECT_URL'];
    # Check if there is a path that matches a database path
    $valid = $this->query("SELECT COUNT(*) AS count FROM `pages` WHERE `path` = ?",array($path))->getResults();
    # If there is a page that is in the database, show that one
    # Presumably this is what you mean by content and id programmically, ie.
    # it's a pre-designated page, not an article that needs to be broken down
    # but follows the same path pattern?
    if($valid['count'] == 1) {
        # Go about doing normal stuff
        # Stop by using die(), exit, or if in function/method, return
        # Idea is to stop at this stage of the script if successful
    }
    else {
        # Explode the query
        $parts = array_filter(explode('/',$path));
        # Check if both parts exist
        if(isset($parts[0]) && isset($parts[1])) {
            # Assign type
            $type = $parts[0];
            # Assign ID
            $id   = $parts[1];
            # Fetch the results (you'll want to sanitize or match from an array the $type value to avoid injection)
            $page = $this->query("SELECT * FROM `{$type}` WHERE `ID` = ?",array($id))->getResults();
            # Check if page is valid and display
            # Stop by using die(), exit, or if in function/method, return
        }      
    }
    
    # Send a 404 header and display a not found
    # Make the last thing happen show a 404 page by default  
    

    Anyway, this is basically what I do, but you can probably do a rewrite that will handle both scenarios from the htaccess file. I personally just have the one that funnels everything and I deal with the path and routing using PHP, then I don't have to create some user control to mess with editing the htaccess file if different scenarios are required down the line.

    点赞 打赏 评论
  • dongshadu2546 2017-07-29 16:24

    Add these lines in .htaccess

    1st case:

    RewriteRule ^article/([0-9]+)$ info.php?id=$1
    

    For second case:

    RewriteRule your-desire-url$ yourpage.php
    
    点赞 打赏 评论

相关推荐 更多相似问题