繁体   English   中英

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文件位于pages文件夹中,profile.php位于页面文件夹中。 希望您能够帮助我。

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

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]

为我做了把戏。 :]

暂无
暂无

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

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