0

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 
4

2 回答 2

0

您可以使用 php 获取 id 值。只需使用以下代码:

   <?php
      $url = $_SERVER['REQUEST_URI'];
      $val = explode("/", $url);
      echo "Your id is: ".$val[2];
   ?>

希望这可以帮助!!!

于 2013-02-11T10:10:18.823 回答
0

您的 .htaccess 重写规则应该是:

RewriteRule ^news/([0-9]+)/([a-zA-Z\-]+)$ /news/index.php?id=$1&title=$2 [QSA,L]

$1$2是根据 和 中包含的表达式定义()。这将向用户显示 url www.example.com/news/5/Test-Title-3,而您的脚本的实际 URL 将是这样,www.example.com/news/index.php?id=5&title=Test-Title-3您将能够像往常一样使用。$_GET['id']$_GET['title']

于 2013-02-11T08:32:46.077 回答