PHP中的URL重写导致问题


URL Rewriting in PHP causing problems

这是我的.htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ profile.php?user=$1 [L,QSA]
</IfModule>

这里是profile.php:

if(isset($_GET['user']) && strlen($_GET['user'])>0) {
    $userreal = $_GET['user'];
    $stmt = $mysqli->prepare("SELECT username FROM users WHERE username=?");
    $stmt->bind_param('s', $userreal);
    $stmt->execute();
    $stmt->store_result();
    if($stmt->num_rows == 1) {
        $user = $_GET['user'];
    }
    else {
        $us = $_SESSION['username'];
        header("Location: profile/" . $us);
        exit();
    }
    $stmt->close();
}
else {
    $us = $_SESSION['username'];
    header("Location: profile/" . $us);
    exit();
}

这是我重定向的部分(认为这可能会有所帮助)。

但它将我重定向到:

http://localhost/PHP/Projects/HassanTech/pages/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/profile/Hassan

当我尝试访问时

http://localhost/PHP/Projects/HassanTech/pages/profile

我想要我的链接:

http://localhost/PHP/Projects/HassanTech/pages/profile?user=Hassan

http://localhost/PHP/Projects/HassanTech/pages/profile/Hassan

我的.htaccess文件在page文件夹中,profile.php在pages文件夹中。希望你能帮助我。

将头重定向更改为使用./,您将留在同一目录中,从而解决问题:

header("Location: ./" . $us);
RewriteEngine On
RewriteRule ^pages/profile/([^/]+)/?$ pages/profile.php?user=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteRule ^(.*)$ $1.php [L]

帮我搞定了。:]