在PHP中造成问题URL重写 [英] URL Rewriting in PHP causing problems

查看:121
本文介绍了在PHP中造成问题URL重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的.htaccess:

 < IfModule mod_rewrite.c>
  RewriteEngine叙述上
  的RewriteCond%{} REQUEST_FILENAME!-f
  的RewriteCond%{} REQUEST_FILENAME!-d
  的RewriteCond%{REQUEST_URI}!= /的favicon.ico
  重写规则^(。*)$ profile.php?用户= $ 1 [L,QSA]
< / IfModule>
 

下面是profile.php:

 如果(使用isset($ _ GET ['用户'])及和放大器; strlen的($ _ GET ['用户'])大于0){
    $ userreal = $ _GET ['用户'];
    $语句= $ mysqli-> prepare(选择用户名从用户其中username =?);
    $ stmt-> bind_param('S',$ userreal);
    $ stmt->执行();
    $ stmt-> store_result();
    如果($ stmt-> NUM_ROWS == 1){
        $ USER = $ _GET ['用户'];
    }
    其他 {
        $ US = $ _SESSION [用户名];
        标题(位置:配置文件/美元);
        出口();
    }
    $ stmt->关闭();
}
其他 {
    $ US = $ _SESSION [用户名];
    标题(位置:配置文件/美元);
    出口();
}
 

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

但它重定向我:

<$p$p><$c$c>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://本地主机/ PHP /项目/ HassanTech /页/简介
 

我想我的链接:

 的http://本地主机/ PHP /项目/ HassanTech /页/配置文件的用户=哈桑
 

 的http://本地主机/ PHP /项目/ HassanTech /页/型材/哈桑
 

我的.htaccess文件是在网页文件夹和profile.php在网页文件夹中。希望你能帮助我。

解决方案

更​​改你的头重定向时使用 ./ 键,你会留在同一个目录从而解决您的问题

 标题(位置:./美元);
 

Here is my .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>

here is 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();
}

This is my redirecting part (thought it might be helpful).

but it redirects me to:

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

when I try to access

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

I want my links:

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

to

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

My .htaccess file is in the pages folder and the profile.php is in the pages folder. Hope you can help me.

解决方案

Change your header redirect to use ./ and you will stay in the same directory thus resolving your problem:

header("Location: ./" . $us);

这篇关于在PHP中造成问题URL重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆