【问题标题】:Rewrite rule with redirect in php在php中使用重定向重写规则
【发布时间】:2012-05-30 20:33:41
【问题描述】:

我似乎对 .htaccess 重写器有疑问。

我想做的是这种形式的调度员:

[website]/[parameter1-text]/[parameter2-text]

我的 .htaccess 中有以下内容:

RewriteRule ^(.*)/(.*)$ dispatcher.php?s1=$1&s2=$2

到目前为止一切顺利。但是 s1 和 s2 是一些需要在数据库(mysql)中匹配的变量,并以以下格式返回为脚本 view.php 提供的真实变量:

view.php?category=[s1]&subcategory=[s2]&objects=5

如何使用 header("Location: /view.php[...]"); 正确重定向它并保持 url 不变(例如:http://example.com/CARS/BLUE http://example.com/SLIPPERS/RED/)

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    首先修复 .htaccess 的一些小问题:

    RewriteRule ^([^/]+)/([^/]*)/?$ dispatcher.php?s1=$1&s2=$2 [L,QSA]
    

    现在在dispatcher.php 里面做你的Mysql 东西并从数据库中获取s1s2 的值。完成并希望在内部将您的请求转发到:view.php?category=[s1]&subcategory=[s2]&objects=5 拥有这种类型的 PHP 代码:

    $_GET['category'] = $value-of-s1-from-DB;
    $_REQUEST['category'] = $_GET['category'];
    $_GET['subcategory'] = $value-of-s2-from-DB;
    $_REQUEST['subcategory'] = $_GET['subcategory'];
    $_GET['objects'] = '5';
    $_REQUEST['objects'] = $_GET['objects'];
    // now include view.php
    // all your parameters are available in view.php in $_GET array
    require_once("view.php");
    

    【讨论】:

      猜你喜欢
      • 2012-08-27
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 2016-06-08
      • 1970-01-01
      相关资源
      最近更新 更多