.htaccess htaccess url重写,但ID更改

.htaccess htaccess url重写,但ID更改,.htaccess,.htaccess,我想知道是否有可能重写一个看起来像这样的url www.example.com/item.php?id=1 to a www.example/item.php without the `?id=1` 请注意,1代表产品id,因此它可能会更改为2或任何数字,具体取决于用户选择的产品 我当前的htaccess 我当前的.htaccess如下所示: RewriteEngine on RewriteBase / RewriteRule ^item/(.*)$ test/main/pages/accou

我想知道是否有可能重写一个看起来像这样的url

www.example.com/item.php?id=1 to a www.example/item.php without the `?id=1`
请注意,1代表产品id,因此它可能会更改为2或任何数字,具体取决于用户选择的产品

我当前的htaccess
我当前的
.htaccess
如下所示:

RewriteEngine on
RewriteBase /
RewriteRule ^item/(.*)$ test/main/pages/account/$1 [L]   // **the item.php file is in the item folder**
ErrorDocument 404 /test/main/pages/general/index.php
Options -Indexes
AuthName "main"
1, 79cb3604-e634-a142-d9cb-5113745b31e2, product1, 59.00
我喜欢这样做

<?php
include('global.php'); ///database connection

function create_guid()
    {
            $microTime = microtime();
        list($a_dec, $a_sec) = explode(" ", $microTime);

        $dec_hex = dechex($a_dec* 1000000);
        $sec_hex = dechex($a_sec);

         ensure_length($dec_hex, 5);
         ensure_length($sec_hex, 6);

        $guid = "";
        $guid .= $dec_hex;
        $guid .= create_guid_section(3);
        $guid .= '-';
        $guid .= create_guid_section(4);
        $guid .= '-';
        $guid .= create_guid_section(4);
        $guid .= '-';
        $guid .= create_guid_section(4);
        $guid .= '-';
        $guid .= $sec_hex;
        $guid .= create_guid_section(6);

        return $guid;

    }

$stmt = $conn->prepare("INSERT INTO `Product (`pid`, `Guid`, `price`) VALUES
(13, '".$guid."', 13)");
$stmt->execute();

?>
使用.htaccess,将“www.example.com/item.php?id=1”重写为“www.example/item.php”是不可能的,因为我们如何知道,像“www.example/item.php”这样的url,产品id是1还是2?这是不可能的。因此,使用.htaccess是不可能的

然而,您可以欺骗并使用PHP和会话变量来进行重写,即使这不是一个好的解决方案。因此,让您的链接处于当前形状下,使用get id参数,只需在item.php文件中添加一个条件,该条件将在会话变量中保存id值,并在id参数不为空时重定向到item.php。

使用.htaccess,将“www.example.com/item.php?id=1”重写为“www.example/item.php”是不可能的,因为,对于像“www.example/item.php”这样的url,我们如何知道产品id是1还是2?这是不可能的。因此,使用.htaccess是不可能的


然而,您可以欺骗并使用PHP和会话变量来进行重写,即使这不是一个好的解决方案。因此,让您的链接处于当前形状下,使用get id参数,只需添加到item.php文件中,如果id参数不为空,则将id值保存在会话变量中并重定向到item.php的一种条件。

由于您的主要动机是让人们不在url中猜测id,并且正如lucas william所指出的那样,您希望的方式在.htaccess中不可能实现,因此您可以存储每个id的id数据库中的产品为guid格式(存储到数据库中的id格式由sugarCRM使用),这也是满足您要求的适当替代品,您可以使用该id唯一标识您的产品表中的每个记录:

创建guid的函数如下所示:

function create_guid()
    {
            $microTime = microtime();
        list($a_dec, $a_sec) = explode(" ", $microTime);

        $dec_hex = dechex($a_dec* 1000000);
        $sec_hex = dechex($a_sec);

         ensure_length($dec_hex, 5);
         ensure_length($sec_hex, 6);

        $guid = "";
        $guid .= $dec_hex;
        $guid .= create_guid_section(3);
        $guid .= '-';
        $guid .= create_guid_section(4);
        $guid .= '-';
        $guid .= create_guid_section(4);
        $guid .= '-';
        $guid .= create_guid_section(4);
        $guid .= '-';
        $guid .= $sec_hex;
        $guid .= create_guid_section(6);

        return $guid;

    }
    function create_guid_section($characters)
    {
        $return = "";
        for($i=0; $i<$characters; $i++)
        {
            $return .= dechex(mt_rand(0,15));
        }
        return $return;
    }

    function ensure_length(&$string, $length)
    {
        $strlen = strlen($string);
        if($strlen < $length)
        {
            $string = str_pad($string,$length,"0");
        }
        else if($strlen > $length)
        {
            $string = substr($string, 0, $length);
        }
    }
另外,我建议您在产品表中保留自动递增字段。 因为在表中维护一个自动递增的字段来唯一标识记录总是一个好主意

我希望这能有所帮助

编辑:

您需要做的是在数据库产品表中添加一个名为“guid”的字段 因此,假设您当前的数据库产品表结构具有以下字段:

id, name, price  //where id is the auto incremented
添加字段guid后,它将变为

id, guid, name, price //where id is auto incremented field and guid uniquely identifies each row in the product table
在数据库产品表中插入产品数据时,使用上述代码生成guid并将其插入。即

比如说

$sql = "Insert into product_table('guid','product_name',product_price) values('".$guid."','product1','59.00');
因此,产品表中的示例数据如下所示:

RewriteEngine on
RewriteBase /
RewriteRule ^item/(.*)$ test/main/pages/account/$1 [L]   // **the item.php file is in the item folder**
ErrorDocument 404 /test/main/pages/general/index.php
Options -Indexes
AuthName "main"
1, 79cb3604-e634-a142-d9cb-5113745b31e2, product1, 59.00
现在在product.php页面中,使用url

yoursite.com/product.php?guid=79cb3604-e634-a142-d9cb-5113745b31e2
而不是使用url

yoursite.com/product.php?id=1
您可以轻松地从数据库产品表中查询与“guid”相关的数据,当然,“guid”还可以唯一地标识数据库中产品表中的每一行,从而消除用户在网页url中猜测您的id的风险


我希望这能让您了解我试图解释的内容。

因为您的主要动机是让人们不去猜测url中的id,而且正如lucas william所指出的那样,您想要的方式在htaccess中是不可能的。相反,您可以将每个产品的id以guid格式存储在数据库中(sugarCRM使用这种存储到数据库中的id格式)这也是满足您要求的合适替代品,您可以使用该id唯一标识您的产品表中的每个记录:

创建guid的函数如下所示:

function create_guid()
    {
            $microTime = microtime();
        list($a_dec, $a_sec) = explode(" ", $microTime);

        $dec_hex = dechex($a_dec* 1000000);
        $sec_hex = dechex($a_sec);

         ensure_length($dec_hex, 5);
         ensure_length($sec_hex, 6);

        $guid = "";
        $guid .= $dec_hex;
        $guid .= create_guid_section(3);
        $guid .= '-';
        $guid .= create_guid_section(4);
        $guid .= '-';
        $guid .= create_guid_section(4);
        $guid .= '-';
        $guid .= create_guid_section(4);
        $guid .= '-';
        $guid .= $sec_hex;
        $guid .= create_guid_section(6);

        return $guid;

    }
    function create_guid_section($characters)
    {
        $return = "";
        for($i=0; $i<$characters; $i++)
        {
            $return .= dechex(mt_rand(0,15));
        }
        return $return;
    }

    function ensure_length(&$string, $length)
    {
        $strlen = strlen($string);
        if($strlen < $length)
        {
            $string = str_pad($string,$length,"0");
        }
        else if($strlen > $length)
        {
            $string = substr($string, 0, $length);
        }
    }
另外,我建议您在产品表中保留自动递增字段。 因为在表中维护一个自动递增的字段来唯一标识记录总是一个好主意

我希望这能有所帮助

编辑:

您需要做的是在数据库产品表中添加一个名为“guid”的字段 因此,假设您当前的数据库产品表结构具有以下字段:

id, name, price  //where id is the auto incremented
添加字段guid后,它将变为

id, guid, name, price //where id is auto incremented field and guid uniquely identifies each row in the product table
在数据库产品表中插入产品数据时,使用上述代码生成guid并将其插入

比如说

$sql = "Insert into product_table('guid','product_name',product_price) values('".$guid."','product1','59.00');
因此,产品表中的示例数据如下所示:

RewriteEngine on
RewriteBase /
RewriteRule ^item/(.*)$ test/main/pages/account/$1 [L]   // **the item.php file is in the item folder**
ErrorDocument 404 /test/main/pages/general/index.php
Options -Indexes
AuthName "main"
1, 79cb3604-e634-a142-d9cb-5113745b31e2, product1, 59.00
现在在product.php页面中,使用url

yoursite.com/product.php?guid=79cb3604-e634-a142-d9cb-5113745b31e2
而不是使用url

yoursite.com/product.php?id=1
您可以轻松地从数据库产品表中查询与“guid”相关的数据,当然,“guid”还可以唯一地标识数据库中产品表中的每一行,从而消除用户在网页url中猜测您的id的风险


我希望这能让您了解我要解释的内容。

对不起,很难理解您想要什么。您想将“www.example.com/item.php?id=1”改写为“www.example.com/item/1”?@Lucas Willems如果我没有很好地举例,那就很抱歉了…我已经编辑了这个问题。我的意思是如果我有一个类似
wwww.example.com/test.php?id=1
的url,如果可以重写它而不必显示
?id=1
好的。现在,我明白了。我会回答你的问题。@LucasWillems请注意
?id=1
将l并不总是
1
它可能会变为2,这取决于
item.php
页面中显示的项目。所以我要说的是,1可能会变为2、12或20,没有ID,如何定位特定的产品,您可以像1_item.php之类的操作,或者创建一个控制器文件,用ID设置会话并重定向到item.php页面。希望它对您有用。抱歉,但很难理解您想要什么。您想将“www.example.com/item.php?id=1”改写为“www.example.com/item/1”?@Lucas Willems如果我没有很好地举例说明,我已经编辑了问题。我的意思是如果