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?

有帮助吗?

解决方案

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:

RewriteRule ^show/student/([^/]*)\.html$ show.php?name=$1 [L]
RewriteRule ^show/category/([^/]*)\.html$ show.php?category=$1 [L]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top