duan7264 2014-12-30 14:07
浏览 176
已采纳

.htaccess错误重定向到另一个目录

I'm having problem with .htaccess that rewrite my url. I created a simple dynamic website and I cant figure how to fix it. This is my login url localhost/foss/ then after successful login the user will be redirected to localhost/foss/main

here is the code of my .htaccess

RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1

and here is my php that handle include files depending on the p value.

<?php
    $page = $_GET['p'];
    $pages = array('home', 'inv_view', 'inv_add', 'inv_delete', 'inv_edit', 's_view', 's_add', 's_delete', 'change_pass', 'register', 'logout');
    if (!empty($page))
    {
        if(in_array($page, $pages))
            include $page . '.php';
        else
            echo 'Page not found!';
    }
    else
        include 'home.php';         
?>

this is working fine, the only weird things that going on, is after the user is redirected to the localhost/foss/main, the .htaccess rewrite the url to localhost/foss/main/?p=main, but when i choose a link like localhost/foss/main/home it will work normally. how can i get rid of the ?p=main after the user login and redirected to localhost/foss/main

  • 写回答

1条回答 默认 最新

  • duanjiao3686 2014-12-30 14:59
    关注

    Reason why this behavior is happening because mod_dir module is running after mod_rewrite and adding a trailing slash in front of directory main/ after your rewrite rule and you get final URL as localhost/foss/main/?p=main.

    To fix this issue have a trailing slash in your refresh tag:

    header('refresh: 5; url = main/'); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题
  • ¥15 echarts图表制作
  • ¥15 halcon根据玻璃面板纹路取区域
  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理