如何使用 htaccess 从 url 中删除查询字符串

问题描述 投票:0回答:0

我正在关注 this 教程,当使用这个 htaccess 代码时,我试图让

domain.com/url/<random string>
工作,带有查询的 url 是
domain.com/url/?u=<random string>

h访问:

RewriteEngine On
RewriteBase /url/

RewriteCond $1 !^(url/index\.php)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ url/index.php?/$1 [L,QSA]

php

if (isset($_GET)) {
    foreach ($_GET as $key => $value) {
        $u = mysqli_real_escape_string($conn, $key);
        $new_url = str_replace('/', '', $u);
    }
    $sql = mysqli_query($conn, "SELECT original_url FROM url WHERE shorten_url = '{$new_url}'");
    if (mysqli_num_rows($sql) > 0) {
        $original_url = mysqli_fetch_assoc($sql);
        header("Location: " . $original_url['original_url']);
    }
}

使用上面的代码我得到一个 404,如果我使用

$value
而不是
$key
它会使用查询 url 重定向我但是没有查询就无法工作。有人能帮我吗?谢谢

php .htaccess apache2.4
© www.soinside.com 2019 - 2024. All rights reserved.