php request vars in sef urls

854 Views Asked by At

I have a page (ie www.mysite.com/products.php?catid=5) in which I use the $_REQUEST[catid'] to get the category id and use it in a query. I switched to SEF urls and now the urls display like www.mysite.com/products/category/5

How can I use the new urls to retrieve the catid value?

The following lines were used in the .htaccess file for switching to SEF urls:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
2

There are 2 best solutions below

1
On BEST ANSWER

You need to rewrite the URL correspondingly using mod_rewrite for example.

RewriteRule ^(.*)$ index.php?url=$1 [NC,QSA]

This rule would rewrite the URL www.mysite.com/products/category/5 to www.mysite.com/index.php?url=products/category/5. From this point on you can parse the variable $_GET["url"] or refine the rewrite rule.

0
On

Since the htaccess takes effect before the PHP starts to build you can grab the current URL with the below code snippet and get the value of the element as follows using the literal example of

www.mysite.com/products/category/5

$currentURL = $_SERVER['REQUEST_URI'];
$elements = explode("/",$currentURL);
$id_variable = $elements[3];

if you include the http:// in the url, then the element count should be 5 I believe

OR ... if you know that the id will always be the last element

$id_variable = $elements[(count($elements)-1)];

that will grab the last element in the array;

you can always use the following to show the data temporarily

echo "<div style='background-color:#FFF;'>";
echo ("Elements array:<br>\n");
print_r($elements);
echo "</div>";

hope this helps