Mod_rewrite在url中返回空格

时间:2014-06-17 21:43:39

标签: php mysql .htaccess mod-rewrite friendly-url

大家好,我的某个网站存在问题。今天我提议将我的所有网址改为更具代表性的东西。我现在有的问题是如何摆脱空间。以下是链接现在的样子“http://website.com/repair/iphone%205s

这是我的.htaccess

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^([a-z]+)\/?$ $1.php [NC,L]
RewriteRule ^smartphone/([a-z]+)/?$ devices.php?d=$1 [L]
RewriteRule ^tablet/([a-z]+)/?$ tablet.php?d=$1 [L]
RewriteRule ^repair/([0-9a-zA-Z_\s]+)/?$ repair.php?m=$1 [L]
RewriteRule ^repair/tablet/([0-9a-zA-Z_\s]+)/?$ repairtablet.php?m=$1 [L]
RewriteRule ^repair/ipod/([0-9a-zA-Z_\s]+)/?$ repairipod.php?m=$1 [L]
</IfModule>

使用我在我的数据库中的每部手机的型号名称构建网址并且它们包含空格,例如iPhone 5s将具有“repair / iphone%205s”的网址这是我的PHP代码,其中我使用_GET声明返回该特定模型的所有修复。

<?php
        $model = mysqli_escape_string($con, $_GET['m']);
        $modelName = mysqli_query($con,"SELECT
        d.id AS 'DeviceID',
        d.model AS 'Model',
        r.id AS 'RepairID',
        r.repair AS 'Repair',
        r.price AS 'Price',
        r.desc AS 'Desc',
        r.active AS 'Active',
        r.img AS 'IMG'
        FROM `tablets` AS d
        LEFT JOIN `tabletrepair` AS r ON
        (d.`id` = r.`pid`)
        WHERE d.`model` =  '$model'
        AND r.`active` = 1 
        ");
    while($row = mysqli_fetch_array($modelName))
    {
          echo $row[Model];
              echo $row[Desc];
              echo $row[Price];
              echo $row[IMG];



    }

    ?>

我想将“http://website.com/repair/iphone%205s”替换为“http://website.com/repair/iphone_5s

感谢任何帮助,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

当您创建WHERE子句时,基于$ _GET ['m']值,您可以使用str_replace用下划线替换所有空格,另外(我不确定是否有必要)您应该解码url($ _GET ['m']包含%20而不是'')。也不记得关于mysql转义或切换到PDO,或其他高级数据库操作库。

$model = str_replace(" ","_",rawurldecode($_GET['m']));

参考

  1. http://www.php.net/manual/en/function.rawurldecode.php
  2. http://pl1.php.net/manual/en/function.str-replace.php