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

图片转代码服务由CSDN问答提供 功能建议

我遇到了重写我的网址的.htaccess问题。 我创建了一个简单的动态网站,我不知道如何解决它。 这是我的登录URL localhost / foss /然后在成功登录后,用户将被重定向到localhost / foss / main

这里是我的.htaccess的代码 \ n

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

这是我的php处理包含文件,具体取决于p值。

 &lt;?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))  
包括$ page。  '.php'; 
 else 
 echo'找不到页面!'; 
} 
其他
包含'home.php';  
?&gt; 
   
 
 

这工作正常,唯一奇怪的事情发生在用户被重定向到localhost / foss / main之后 .htaccess将url重写为localhost / foss / main /?p = main,但是当我选择localhost / foss / main / home这样的链接时,它会正常工作。 用户登录后如何摆脱?p = main并重定向到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/'); 
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题