简体   繁体   English

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

[英]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. 我有这个网址的问题,我想知道如何将其重写为htaccess。 I'm pretty new into this stuff so I'm curious on how to do this, for example this url: WEBSITE/?page=online 我对这些东西很陌生,所以我很好奇如何做到这一点,例如这个网址:WEBSITE /?page = online

and rewrite it to, for example: WEBSITE/page/online 并将其重写为,例如:WEBSITE / page / online

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

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

I use this PHP script to include the pages inside one HTML document: 我使用这个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';
        }
    }

?>

You could do something like this. 你可以这样做。

.htaccess file .htaccess文件

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

This says take every request coming to your server and sends it into the control.php file. 这表示将每个请求发送到您的服务器并将其发送到control.php文件。 Then in control.php we process all the gets to directories. 然后在control.php中我们处理所有目录的获取。 You'll probably want some fallbacks, eg no GET request; 你可能想要一些后备,例如没有GET请求; should it go to the root, 404, 403? 应该去根,404,403?

control.php control.php

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

In your .htaccess file 在.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