我的主页的PHP URL

时间:2014-07-24 07:12:58

标签: php url

所以我正在编辑一个PHP站点(我是一个Python人)。我需要的是一种可能重定向的方式,以便当有人导航到该站点时,例如www.mysite.com,应该提供主页。用于提供其他页面的系统非常简单:我们使用www.mysite.com?page_id=contact-us来浏览联系人页面。查询字符串有助于服务器端代码知道要提供的页面。所以我想要的是,当用户通过键入www.mysite.com导航到网站时,他应该转到页面www.mysite.com?page_id=home

谢谢。

示例代码:

$page = isset($_GET['page_id']) ? $_GET['page_id'] : null;
    if ($page !== null) {
            //redirect to correct page
        require("modules/inside.php");
    } else {
            //redirect to 'home'
        header('Location: https://www.ndovucard.com?page_id=home');
        require("modules/home.php");
    }

4 个答案:

答案 0 :(得分:1)

...试

if (!isset($_GET['page_id'])){
   header('Location: www.mysite.com?page_id=home');
}

答案 1 :(得分:0)

这样的东西?

$page = (isset($_GET['page_id'])) ? $_GET['page_id'] : 'home';
echo $page; // 'home' or provided page_id

答案 2 :(得分:0)

从你所说的话来看,你需要一个:

if (!isset($_GET['page_id'])) $_GET['page_id'] = 'home';

我个人更喜欢完整的URL重写系统而不是URL查询,因为它相信搜索引擎更喜欢contact-us.html而不是page_id = contact-us

编辑: 你也可以这样做:

if (!isset($_GET['page_id'])) {
    header('Location: /?page_id=home');
    die(); // stop any further processing
}

答案 3 :(得分:0)

类似的东西:

$page = isset($_GET['page_id']) ? $_GET['page_id'] : 'home';
/* check that page is a valid page else serve error page */
/* then redirect to the correct $page */
header('Location: www.mysite.com?page_id=' . $page);
exit();

顺便说一下,在重定向之前,您需要某种方法来检查$_GET['page_id']是否始终是有效的page_id