按变量重写URL

时间:2016-01-28 09:39:52

标签: php apache rewrite

我有一个搜索表格:
1)搜索(所有时间设置 - 它是一个选择='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.'/">';
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

<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。