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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条