htaccess重写不能在地址栏中正确重写

时间:2013-09-26 11:11:55

标签: regex .htaccess mod-rewrite url-rewriting

的.htaccess

RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ http://localhost/website/sale/phones/index.php?type=$1&location=$2
RewriteRule ^([A-Za-z0-9-]+)/$                 http://localhost/website/sale/phones/index.php?type=$1

我设置了像这样的链接

if (!isset($_GET['type'])) {
    $query = "SELECT type, url FROM {$phones}";

    $result = mysql_query($query);

    while($phone = mysql_fetch_assoc($result)) {
        echo '<li><a href="'. $phone['url'] .'/">'. $phone['type'] .'</a></li>';
    }
} else {
    $query = "SELECT location, url FROM locationstwo LIMIT 100";

    $result = mysql_query($query);

    while($location = mysql_fetch_assoc($result)) {
        echo '<li><a href='. $_GET['type'] .'/'. $location['url'] .'/>'. $location['location'] .'</a></li>';
    }   
}

&GT;

网址在左下角显示为localhost / website / sale / phones / phonetype / location /

但点击链接后,地址栏会出现?变量

本地主机/网站/销售/电话/ index.php的类型= phone型&安培;位置=位置 当我真正想要的是localhost / website / sale / phones / phone-type / location /

是否与.htaccess有关?

说,当显式键入/ phone-type / location /它传递参数时,所以htaccess正在工作,但地址栏只显示了?varialbe。

我该如何解决这个问题?感谢。

1 个答案:

答案 0 :(得分:1)

这是由于重写规则不正确造成的。确保使用不带域的路径作为目标。用以下代码替换您的代码:

RewriteRule ^([\w-]+)/([\w-]+)/?$ /website/sale/phones/index.php?type=$1&location=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ /website/sale/phones/index.php?type=$1 [L,QSA]