重定向而不更改浏览器URL

时间:2014-08-02 09:31:50

标签: php regex .htaccess mod-rewrite apache2

我有以下网址,

www.example.com/home.php and
 www.example.com/login.php

当我从php重定向到任何这些页面时,浏览器的网址应保留为www.example.com。

我尝试过,但由于缺乏mod_rewite的知识,我无能为力。

请提前帮助,请提供帮助

3 个答案:

答案 0 :(得分:4)

如果您想在登录后保留页面,在用户未登录时使用相同的URL(我们假设www.example.com),那很简单:

在www.example.com中加载index.php,因此我们应该更改index.php的内容。

当用户未登录时,首先访问它:

include("htmls/index.html"); // index.html could contain whatever you like

但是你添加了这个条件,上面一行是在一个条件中:

if(isset($_POST['form-submit']))
{
   // do the login process and if success
   include("htmls/index_loggedin.html");
}
else if(is_user_logged_in()===true)
{
  include("htmls/index_loggedin.html"); // 
}
else
{
  include("htmls/index.html"); // means the user has not submitted anything and is also not logged in

}

上面的代码说如果用户已登录,则处理他/她的登录然后包含 一个用于登录用户的视图,如果用户已经登录,也向他/她显示用于登录用户的视图,否则,如果用户未登录,也没有发送登录请求,则向他展示未登记用户的基本视图。我假设您的表单的提交按钮名为" form-submit"。

然而,上述命名仅仅是为了清楚起见。例如,您可以将index.html和index_loggedin.html组合到一个view_index.php中,然后还将主index.php的条件复制到view_index.php。

最后一点需要注意的是,您应该尽可能完整地分离代码和视图。

答案 1 :(得分:1)

你不能用mod_rewrite做到这一点。如果你想要home.php或login.php,Apache应该怎么知道?您可能希望使用AJAX在后台加载文件,然后显示内容。

答案 2 :(得分:0)

当用户在页面之间导航时,您应该在页面布局中使用iframe html标记作为浏览器的网址www.example.com。不重定向或重写不适合这些目的。

相关问题