我有一个问题与我的Nginx重写规则。
我有两个页面:
- 首页:category.php
- 第二页:subcategory.php
My Page:
<!doctype html>
<html>
<head></head>
<body>
<ul>
<?php
while($row = $smtp->fetch(PDO::ASSOC)){
echo "li><a href='category.php? id='".$row['id']."'>'".$row['name']."'</a></li>";
}
?>
</ul>
<?php
while($row = $smtp->fetch(PDO::ASSOC)){
echo "li><a href='subcategory.php?id='".$row['id']."'>'".$row['name']."'</a></li>";
}
?>
</body>
</html>
我的问题是,如果我推一个链接与category.php?id=3
。我将得到与其他值相同的页面,但subcategory.php
的链接看起来不同,没有subcategory.php?id=1
看起来像category/subcategory/2
FireBug显示正确的url: subcategory.php?id=1
。
为什么?
我的重写规则:rewrite '^/category.php/(.*)$' /subcategory.php?id=$1 last;
我需要一个像subcategory/1
而不是category/subcategory/1
的链接
这里有一些小问题。
要回答您的问题,第一部分是使用href中的url。你使用的是相对URL,相对URL会被添加到浏览器认为是当前URL的目录中。
如果你在http://yourdomain.com/category/[something]
,那么href="subcategory.php?id..."
会把你送到http://yourdomain.com/category/subcategory.php?id...
——因为当前的ULR目录是http://yourdomain.com/category/
即使这个链接是虚拟的(即在。haccess中修改),浏览器也不知道这个,它是浏览器创建链接的。
你的解决方案是使用绝对url href="/subcategory.php?id..."
,因为这会将你发送到http://yourdomain.com/subcategory.php?id...
重定向中出现另一个问题。
您要查找的匹配项:
http://yourdomain.com/category.php/[Something]
但是我猜你想要匹配的是:
http://yourdomain.com/category/[something]
希望对你有帮助。