【问题标题】:Rewrite URL by variables通过变量重写 URL
【发布时间】:2016-01-28 09:39:51
【问题描述】:

我有一个搜索表单:
1) 搜索(所有时间设置 - 这是一个选择表单,selected='true')
2) 输入(仅接受 A-Z、a-z、0-9)
3) 和其他 2 选择表单,比如第一个

您可以搜索所有 3 个变量(第 2 点除外),我想为 3 个变量重写 URL。 当我有所有变量重写看起来像:

RewriteRule ^servers/([a-z-]+)/([A-Za-z0-9-]+)/([a-z-]+)/([A-Z-]+)/?$   servers.php?query=$1&matching=$2&playing=$3&location=$4 [NC,L]  

我试过了

RewriteRule ^servers/([a-z-]+)/([a-z-]+)/([A-Z-]+)/?$   servers.php?query=$1&playing=$2&location=$3 [NC,L]  

但不起作用。你能帮帮我吗?

<?php 
 if(isset($_POST['searchservers'])){
    $okGame = 0;
    $okLocation = 0;
    //Search Type Filter
    switch($_POST['searchtype']){
        case 'nameorip':
        case 'map':
            $query = $_POST['searchtype'];
        break;
        default:
            $query = "nameorip";
        break;
    }
    // Sentence Filter
    if(isset($_POST['matching'])){
        $matching = preg_replace('/[^A-Za-z0-9\-]/', '', $_POST['matching']);
    }
    // Game Filter
    $getGames = DB::conn()->query("SELECT abbr FROM `games`");
        while($gamee = $getGames->fetch_assoc()){
            if($_POST['game'] == $gamee['abbr']){
                $playing = $gamee['abbr'];
                $okGame = 1;
            }
        }
        if($okGame == 0){
            $playing = "allgames";
        }
        // Location Filter
        if(strlen($_POST['location']) == 2){
            $location = preg_replace('/[^A-Z\-]/', '', $_POST['location']);
            $okLocation = 1;
        }
        if($okLocation == 0){
            $location = "alllocations";
        }
        if(!isset($_POST['matching'])){
            echo '<meta http-equiv="refresh" content="0;url=/servers/'.$query.'/'.$playing.'/'.$location.'/">';
        } else {
            echo '<meta http-equiv="refresh" content="0;url=/servers/'.$query.'/'.$matching.'/'.$playing.'/'.$location.'/">';
        }
    }
?>

【问题讨论】:

  • 实际查看表单以及了解表单的提交方式可能会很方便,因为通常当您通过 GET 提交表单时,它的格式为 var=value&var2=val2 等比 val/val2/val3 等,所以我怀疑你想要做的是动态重定向到一个漂亮的 url,给定一个带有典型查询字符串的标准表单提交?
  • 没有。我的表单具有 POST 方法,因为首先,在非空输入的情况下,我更喜欢将 preg_match 用于符号(如 $%@ 等)。然后我检查第二个和第三个选择表单是否有特定的表单。最后,在符号的 preg_match 之后,我检查该输入是否为空或包含空格(使用 ctype_space)。然后我这样做:if(!isset($_POST['matching'])){echo '&lt;meta http-equiv="refresh" content="0;url=/servers/'.$query.'/'.$playing.'/'.$location.'/"&gt;';} else { echo '&lt;meta http-equiv="refresh" content="0;url=/servers/'.$query.'/'.$matching.'/'.$playing.'/'.$location.'/"&gt;';} -No space left-
  • 所以您使用 post 提交,然后使用 php 重定向并生成所需的 url 结构?添加表单和一些逻辑以重定向到问题而不是评论会很好。
  • 我在第一篇文章中添加了代码。谢谢!

标签: php apache rewrite


【解决方案1】:

尝试以下操作:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^servers/([a-z-]+)/([a-z-]+)/([A-Z-]+)/?$ servers.php?query=$1&playing=$2&location=$3 [NC,L] 
RewriteRule ^servers/([a-z-]+)/([A-Za-z0-9-]+)/([a-z-]+)/([A-Z-]+)/?$ servers.php?query=$1&matching=$2&playing=$3&location=$4 [NC,L] 
</IfModule>

当我导航到 http://localhost/servers/a/b/c/d/http://localhost/servers/a/b/c/ 时,它似乎工作了

确保您的根目录中确实有 servers.php 或相应地更改 Rewritebase。

【讨论】:

  • 如果我拥有所有 4 个变量,我的 Rewrite 系统工作正常!我想Rewrite 获取 3 个变量。再次阅读第一篇文章和 cmets。
猜你喜欢
  • 2014-02-04
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
相关资源
最近更新 更多