This is my default dynamic link:
www.example.com/news/index.php?id=5&title=Test-Title-3
This is the link that have been rewritten by htaccess:
www.example.com/news/5/Test-Title-3
Now I wish to get the id variable (which is 5) and/or title variable (which is Test-Title-3) with PHP.
I have tried using $_GET['id']
but returns
index.php/Test-Title-3
or using $_GET['title'] which returns nothing. Any idea how to get the variable?
(Edited) This is my .htaccess file, in case of need:
Options +FollowSymLinks Options All -Indexes
<files .htaccess>
order allow,deny
deny from all
</files>
RewriteEngine on
RewriteRule (.*)/(.*)/ index.php?id=$1&title=$2
Your .htaccess rewrite rule should be:
RewriteRule ^news/([0-9]+)/([a-zA-Z\-]+)$ /news/index.php?id=$1&title=$2 [QSA,L]
$1
and $2
are defined based on the expression contained within (
and )
. This will show the url www.example.com/news/5/Test-Title-3
to the user while your script's actual URL will be www.example.com/news/index.php?id=5&title=Test-Title-3
so you will be able to use $_GET['id']
and $_GET['title']
as you normally would.
you can get the id value using php. Just use the following code:
<?php
$url = $_SERVER['REQUEST_URI'];
$val = explode("/", $url);
echo "Your id is: ".$val[2];
?>
Hope this helps!!!