Php 由htaccess重写时获取参数
这是我的默认动态链接: www.example.com/news/index.php?id=5&title=Test-title-3 这是htaccess已重写的链接: www.example.com/news/5/Test-Title-3 现在我想用PHP获取id变量(5)和/或title变量(Test-title-3) 我尝试使用Php 由htaccess重写时获取参数,php,url,variables,dynamic,Php,Url,Variables,Dynamic,这是我的默认动态链接: www.example.com/news/index.php?id=5&title=Test-title-3 这是htaccess已重写的链接: www.example.com/news/5/Test-Title-3 现在我想用PHP获取id变量(5)和/或title变量(Test-title-3) 我尝试使用$\u GET['id']但返回 index.php/Test-Title-3 或者使用$\u GET['title'],它不返回任何内容。知道如何得到这个变量吗
$\u GET['id']
但返回
index.php/Test-Title-3
或者使用$\u GET['title'],它不返回任何内容。知道如何得到这个变量吗
(编辑)
这是我的.htaccess文件,以备需要:
Options +FollowSymLinks Options All -Indexes
<files .htaccess>
order allow,deny
deny from all
</files>
RewriteEngine on
RewriteRule (.*)/(.*)/ index.php?id=$1&title=$2
Options+FollowSymLinks选项所有-索引
命令允许,拒绝
全盘否定
重新启动发动机
重写规则(.*)/(.*)/index.php?id=$1和title=$2
您的.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
,因此您将能够像通常一样使用$\u GET['id']
和$\u GET['Title']
。您可以使用php获取id值。只需使用以下代码:
<?php
$url = $_SERVER['REQUEST_URI'];
$val = explode("/", $url);
echo "Your id is: ".$val[2];
?>
希望这有帮助 您需要在RewriteRule(.*)/(.*)/index.php结尾添加[QSA]标志?id=$1&title=$2[QSA]嘿,谢谢!它是有效的,我觉得错过那个旗子真的很愚蠢。我确实尝试过使用你的规则,但突然它返回错误404。也许我也应该更改我的链接href?没必要,正则表达式中有一个bug。它现在可以工作了(通过测试)