网站未正确重定向

时间:2015-09-05 02:53:28

标签: php apache .htaccess mod-rewrite

我正在创建一个strawpoll网站(有点像strawpoll.me,但您可以设置轮询到期并使用Cookie),可以在http://www.makepoll.co.uk找到。

问题在于,当您转到它时,我们会在http://www.makepoll.co.uk/1转到http://www.makepoll.co.uk/home时被退回。

我的htaccess文件如下所示:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-z]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^([a-z]+)/([A-Z0-9]+)/?$ index.php?p=$1&s=$2 [L,QSA]
</IfModule>

我的index.php页面如下所示:

<?php require("backend/index.php");
if(empty($page)==TRUE)
{
    header("Location: home");
    die();
}
?><!DOCTYPE html>
<html>
<head>
<?php if(empty($part)==FALSE)
{
    echo "<link rel=\"stylesheet\" href=\"../frontend/jquery-ui.css\">\n";
    echo "<link rel=\"stylesheet\" href=\"../frontend/styles.css\">\n";
    echo "<script src=\"../frontend/jquery.js\"></script>\n";
    echo "<script src=\"../frontend/jquery-ui.js\"></script>\n";
}
else
{
    echo "<link rel=\"stylesheet\" href=\"frontend/jquery-ui.css\">\n";
    echo "<link rel=\"stylesheet\" href=\"frontend/styles.css\">\n";
    echo "<script src=\"frontend/jquery.js\"></script>\n";
    echo "<script src=\"frontend/jquery-ui.js\"></script>\n";
}
?>
<script>
$(function() {
    $( "#datepicker" ).datepicker();
});
</script>
</head>
<body>
<?php if(empty($part)==FALSE)
{
    echo "<a href=\"#content\" tabindex=\"", $tab++, "\"><img src=\"../frontend/content.png\" alt=\"Skip to Content\"></a>\n";
}
else
{        
    echo "<a href=\"#content\" tabindex=\"", $tab++, "\"><img src=\"frontend/content.png\" alt=\"Skip to Content\"></a>\n";
}
?>
<div id="pollbox">
<?php if(in_array("{$page}.php", $content)==TRUE)
{
    include("{$basedir}/content/{$page}.php");
}
else
{
    echo "<p>Page not found, I'm sorry</p>";
    http_response_code(404);
}
?>
</div>
<footer>
<ul>
<li><a href="../home/1" tabindex="<?php echo $tab++; ?>">Create New Poll</a></li>
<li><a href="../terms/1" tabindex="<?php echo $tab++; ?>">Terms of Service</a></li>
<li><a href="../terms/2" tabindex="<?php echo $tab++; ?>">Privacy Policy</a></li>
<li>Contact Us</li>
</ul>
</footer>
</body>
</html>

后端/ index.php页面如下所示:

<?php /* This file will contain the backend functions and set things up, such as the MySQL connection, and set the variables the site is going to use. This file line will connect to the SQL database. If there is a connection error, the script will be killed, and since there is no point continuing, a message will be displayed */
ob_start();

session_start();
$connection = new mysqli("localhost", "root", "", "example");
if($connection->connect_error)
{
    die("Sorry... I was unable to connect");
}
$basedir             = dirname(__FILE__);                         // This variable will hold the path to the current folder
$page                = strtolower($_GET["p"]);                    // This variable will hold the current page name
$part                 = $_GET["s"];                             // This variable will keep track of which part of the script the user is on
$tab                 = 1;                                                     // This variable will hold the tabindex
$content            = scandir("{$basedir}\content");        // This will scan the content folder and return an array 
unset($content[0], $content[1]);                                // This will unset the 0th and 1st element of the array 
include("{$basedir}/functions.php");                        // This will include the backend functions script
?>

你们能给我一些指示吗?就我而言,它应该重定向到/ home

2 个答案:

答案 0 :(得分:1)

将此重写规则添加到列表

RewriteRule ^([a-z]+)/home$ index.php?p=$1 [L,QSA]

希望这有帮助

答案 1 :(得分:0)

我猜你可以使用/的最后一个绝对路径解决这个问题。像这样:

标题(&#39;位置:http://www.makepoll.co.uk/home/&#39;);

我不明白为什么你的网址会附加一个。

此外,您需要重构代码并重新考虑您的架构,但这是另一个问题。)