1 个动态页面的 2 个 url 重写规则
我有一个名为 show.php 的动态页面。该页面是动态的,URL 可以是 show.php?name=john-doe
或 show.php?category=student
。
我正在尝试创建一个重写规则,将 url 转换为 /show/john-doe.html
(名称)或 /show/student.html
(类别)。
到目前为止,这就是我的 .htaccess 中的内容。
RewriteRule ^show/([^/]*)\.html$ show.php?name=$1 [L]
RewriteRule ^show/([^/]*)\.html$ show.php?category=$1 [L]
目前,只有名称规则有效,但类别规则无效。怎么了?
I have a dynamic page called show.php. The page is dynamic and the url may be either show.php?name=john-doe
or show.php?category=student
.
I'm trying to create a rewrite rule that turns the url into /show/john-doe.html
for names or /show/student.html
for category.
This is what I have in my .htaccess so far.
RewriteRule ^show/([^/]*)\.html$ show.php?name=$1 [L]
RewriteRule ^show/([^/]*)\.html$ show.php?category=$1 [L]
Currently, only the name rule works but the category rule doesn't. What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您将所有 show/xxx.html 发送到同一 URL(第一个 URL)。由于两个重写规则使用完全相同的参数,因此只有第一个规则有效。
您可以通过两种不同的方式解决这个问题。
您可以使用 show.php?id=xxx 并在 PHP 中接受名称和类别,并确定要显示的页面。
或者您在重写中使用两种不同类型的网址来获取 show/category/student.html 和 show/student/john-doe.html,如下所示:
The problem is that you are sending all show/xxx.html to the same URL (the first one). Since both rewrite rules are using exactly the same parameter only the first one will work.
You could solve this in two different ways.
Either you use show.php?id=xxx and accept both name and category in your PHP and detirmine there what page to show.
Or you use two different types of urls in your rewrite to get show/category/student.html and show/student/john-doe.html like so: