【问题标题】:Mod_rewrite returning spaces in urlMod_rewrite 在 url 中返回空格
【发布时间】:2014-08-08 01:19:27
【问题描述】:

大家好,我的一个网站有问题。今天我决定将我所有的网址更改为更美观的东西。我现在遇到的问题是如何摆脱空格。这是链接现在的样子“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>

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

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

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

【问题讨论】:

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


    【解决方案1】:

    当您创建 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

    【讨论】:

    • 感谢您解决了我的问题,是的,我很快就会将所有内容切换到 PDO。再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多