繁体   English   中英

url重写,htaccess和PHP如何做到这一点

[英]Url rewrite, htaccess and PHP how to do it

我有这个网址的问题,我想知道如何将其重写为htaccess。 我对这些东西很陌生,所以我很好奇如何做到这一点,例如这个网址:WEBSITE /?page = online

并将其重写为,例如:WEBSITE / page / online

或者这个网址:网站/?page = account&hide = x

例如:WEBSITE / page / account / hide / x

我使用这个PHP脚本将页面包含在一个HTML文档中:

    <?php

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

?>

你可以这样做。

.htaccess文件

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

这表示将每个请求发送到您的服务器并将其发送到control.php文件。 然后在control.php中我们处理所有目录的获取。 你可能想要一些后备,例如没有GET请求; 应该去根,404,403?

control.php

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

在.htaccess文件中

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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