简体   繁体   中英

Url rewrite, htaccess and PHP how to do it

I have a problem with this url and I want to know how to rewrite that into htaccess. I'm pretty new into this stuff so I'm curious on how to do this, for example this url: WEBSITE/?page=online

and rewrite it to, for example: WEBSITE/page/online

or this url: WEBSITE/?page=account&hide=x

to, for example: WEBSITE/page/account/hide/x

I use this PHP script to include the pages inside one HTML document:

    <?php

    if (empty($_GET)) 
    {
        include 'pages/index.php';
    } else {
        if (!file_exists('pages/' . $_GET['page'] . '.php' ))
        {
            header("Location: /");
        } else {
            include 'pages/' . $_GET['page'] . '.php';
        }
    }

?>

You could do something like this.

.htaccess file

RewriteEngine On
RewriteRule (.*) control.php [L]

This says take every request coming to your server and sends it into the control.php file. Then in control.php we process all the gets to directories. You'll probably want some fallbacks, eg no GET request; should it go to the root, 404, 403?

control.php

<?php
$parts = '';
foreach($_GET as $key => $directories) {
    $parts .= $key . '/' . $directories . '/';
}
header('Location: ' . '/' . $parts);
?>

In your .htaccess file

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Match SITE/page/account/hide/x, etc
RewriteRule ^/?page/([^\.*]+)/([^\.*]+)/([^\.*]+)?$ index.php?page=$1&$2=$3 [L,QSA]

# Match /SITE/page/online etc.
RewriteRule ^/?page/([^\.*]+)/?$ index.php?page=$1 [L,QSA]

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