php $ _GET值无法传递给mod_rewrite

时间:2014-01-04 17:31:01

标签: php apache .htaccess mod-rewrite url-rewriting

在我发布此问题之前,我已经在SO和Google中进行了搜索,最重要的是我还使用了mod_rewrite generator

然而,我把它放在我的.htaccess中的结果似乎没有起作用。

我当前的网址: http://localhost/jeff/test/result.php?start=2-1-2014&end=2-1-2014&submit=Go

我想要的输出: http://localhost/start/2-1-2014/end/2-1-2014

我从mod_rewrite生成器获得的代码:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+/jeff/test/result\.php\?start=([^&]+)&end=([^&\s]+) [NC]
RewriteRule ^ /start/%1/end/%2? [R=302,L]

RewriteRule ^start/([^/]+)/end/([^/]+)/?$ /jeff/test/result.php?start=$1&end=$2&submit=Go [L,QSA]

现在每次按下提交按钮,它仍然会将我返回到旧网址,而不是根据.htaccess重写网址

旁注:

  • 我的XAMPP已取消注释mod_rewrite引擎
  • 我已经用其他项目测试了mod_rewrite模块并且它正在运行
  • 我尝试重启apache一段时间,但我无法得到结果

我哪里出错了?我相信发电机能给我正确的结果。

这是我的提交表单代码:

<form method="get" action="result.php">
From: <input type="text" name="start" /><br />
End: <input type="text" name="end" /><br />
<input type="submit" value="Go" />
</form>

和我的result.php代码:

error_reporting(E_ALL); ini_set('display_errors', 1);
// bind column to variable
//Connect to DB
$host = "localhost"; //Host Name
$port = '3306'; //Default MySQL Port
$dbname = "mama"; //Database Name
$db_username = "root"; //MySQL Username
$db_password = ""; //MySQL Password

$dsn = "mysql:host=$host;port=$port;dbname=$dbname"; //Data Source Name = Mysql
$db = new PDO($dsn, $db_username, $db_password); //Connect to DB

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try {
  $start_date = $_GET['start'];
  $end_date = $_GET['end'];
  $start = DateTime::createFromFormat('d-m-Y', $start_date);
  $end = DateTime::createFromFormat('d-m-Y', $end_date);
  $query = $db->prepare("SELECT * FROM log WHERE log_datetime >= :start AND log_datetime < :end + INTERVAL 1 DAY");
  $success = $query->execute(array(':start'=>$start->format('Y-m-d'), ':end'=>$end->format('Y-m-d')));

}
// Catch any exception thrown
catch (PDOException $e) {
  echo "Error: " . $e->getMessage();
  // Exit, redirect, whatever you need to do.
}
$rows = $query->fetchAll(PDO::FETCH_OBJ);

我当前设置的屏幕截图: screenshot

1 个答案:

答案 0 :(得分:0)

没有RewriteBase错误,不适合你。

将此代码放入 DOCUMENT_ROOT/.htaccess文件

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+/jeff/test/result\.php\?start=([^&]+)&end=([^&\s]+) [NC]
RewriteRule ^ /start/%1/end/%2? [R=302,L]

RewriteRule ^start/([^/]+)/end/([^/]+)/?$ /jeff/test/result.php?start=$1&end=$2&submit=Go [L,QSA]