重写规则映射到URL参数没有扩展


Rewrite Rule to map to URL Parameter without extension

我试图使用HTACCESS重写以下url:

http://example.com/index.php?item_id=123456

我想把它重写为:

http://example.com/213456

参数item_id将在我的php文件中使用以下代码捕获:

<?php
    if (isset($_GET['item_id'])) {
        $url_item = $_GET['item_id'];
        $urlParmResult = filter_var($url_item, FILTER_SANITIZE_URL);
        echo $urlParmResult;
    }else{
        echo "The link you requested does not exist.";
    }
?>

我使用下面的。htaccess:

RewriteEngine On
RewriteRule ^([^/]*)$ index.php?item_id=$1 [L]

然而,我在页面上返回的是:

index.php
有人能帮我一下吗?我哪里做错了?

您可以使用:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?item_id=$1 [L]

删除现有的文件或目录,避免索引重定向到索引