我应该如何编写规则使其看起来像这样?

时间:2020-10-13 15:39:47

标签: php

我试图弄清楚如何转换网址。它会转到索引页面。

我定义的常数 define("URL_PAGE", "page.php?p=");

链接: <a class="menu-link" href="'.BASE_URL.URL_PAGE.$row1['page_slug'].'">';

.htaccess RewriteRule ^page/(.*)$/?$ page.php?p=$1 [NC,L]

结果: http://localhost/aione/page.php?p = about-us

我正试图抓住这样的传入链接。

if(!isset($_REQUEST['p']))
{
    header('location: index.php');
    exit;
}
else
{
    // Check the page slug is valid or not.
    $statement = $pdo->prepare("SELECT * FROM mbo_page WHERE page_slug=? AND status=?");
    $statement->execute(array($_REQUEST['p'],'Active'));
    $total = $statement->rowCount();
    if( $total == 0 )
    {
        header('location: index.php');
        exit;
    }
}

我应该如何编写规则使其看起来像这样?感谢您的帮助。 http:// localhost / aione / page / about-us

1 个答案:

答案 0 :(得分:0)

这似乎是一个琐碎的问题。

尝试:

<a class="menu-link" href="'.BASE_URL.'page/'.$row1['page_slug'].'">';

或者,您可以尝试以下操作:

define("URL_PAGE", "page/");

您的.htaccess文件需要看起来像这样:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/*page/([^/]*)$ page.php?id=$1 [L,QSA] #If static R=301
</IfModule>

.htaccesspage.php放在同一目录(即文件夹)中。 (例如在http://localhost/aione/中)

注意:

  • ^/*page/([^/]*)$ page.php?id=$1 使用以下样式:“ http:// localhost / aione / page / about-us”,如果您希望将类似about-us/test之类的内容也作为page.php?id=about-us/test传递,请使用以下规则:

  • ^/*page/(.*)$ page.php?id=$1 也匹配“ http:// localhost / aione / page / about-us / test / link”之类的内容,它将重写为:page.php?id=about-us/test/link,表示以下含义:

$_GET['id'] === 'about-us/test/link'; // true

一如既往,请记住验证并清除任何用户输入。