简体   繁体   中英

PHP URL for my homepage

so I'm editing a PHP site (I'm a Python guy). What I need is a way to maybe redirect such that when someone navigates to the site eg www.mysite.com , the homepage should be served. The system used to serve other pages is as sush: to navigate let's say to the contacts page, we use www.mysite.com?page_id=contact-us . The query string helps the server side code to know what page to serve. So what I want is that when a user navigates to the site by typing www.mysite.com , he should get to the page www.mysite.com?page_id=home .

Thank you.

Sample code:

$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");
    }

Try...

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

Something like this?

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

From what you've said it sounds like you need a:

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

Personally I prefer a full URL rewrite system rather than passed by URL query as it's believed search engines prefer contact-us.html rather than page_id=contact-us

Edit: You could alternatively do:

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

something like:

$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();

By the way, you need some way to check that $_GET['page_id'] is always a valid page_id before redirecting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM