remixdesign remixdesign - 1 month ago 13
PHP Question

Rewrite Rule to map to URL Parameter without extension

I am attempting to use HTACCESS to rewrite the following url:

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


I would like it rewritten as:

http://example.com/213456


The parameter item_id is to be captured in my php file with the following code:

<?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.";
}
?>


I am using the following .htaccess:

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


however all i get returned on the page is:

index.php


Can someone please help me out? What am I doing wrong here?

Answer

You can use:

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

Which eliminates existing files or directories, to avoid redirect index to index