.htaccess redirect to another directory
I have a problem with .htaccess which is rewriting my url. I've created a simple dynamic website and I can't figure out how to fix it. This is my login url localhost / foss / after successful login the user will be redirected to localhost / foss / main
here is my .htaccess code
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 handles the 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 works fine, only the weirdest things that happen is after the user is redirected to localhost / foss / main .htaccess rewrites url to localhost / foss / main /? p = main, but when I select a link like localhost / foss / main / home it works fine. how can i get rid of? p = main after user login and redirected to localhost / foss / main
source to share
The reason this behavior is happening is because the module mod_dir
runs after mod_rewrite
and adds a trailing slash in front of the directory main/
after your rewrite rule and you get the final URL as localhost/foss/main/?p=main
.
To fix this problem, you have a trailing slash in your update tag:
header('refresh: 5; url = main/');
source to share