.htaccess: Problem with URL rewriting

2019-07-31 01:57发布

I'm new to url rewriting and having a problem i can't figure out.

I got this 2 conditions:

RewriteRule ([^/]+).php index.php?com=cat&catname=$1 [L]
RewriteRule ([^/]+)/([^/]+).php index.php?com=detail&catname=$1prodname=$2 [L]

and need 2 urls like this:

website.com/category-name.php
website.com/category-name/product-name.php

It seems that the first condition rules upon the second... i mean: if i call the first url everything works fine, but when i call the second url i can't get variables as i want ("com" is always "cat" and "catname" get the value of $2)

Thanks in advance!

1条回答
我只想做你的唯一
2楼-- · 2019-07-31 02:27

URLs that match the second rule will also match the first rule. As the first rule is marked "L", the second rule will never be applied.

Maybe you should match absolute URLs - begin the regex with ^/ to match the beginning of a URL, and end it with $ to match the end of the URL. Remember that rewrite rules are applied to the URL path (everything that follows website.com, including the slash).

For example (didn't test this of course):

# Example: website.com/books.php -> website.com/index.php?com=cat&catname=books
RewriteRule ^/([^/]+).php$ /index.php?com=cat&catname=$1 [L]
# Example: website.com/books/java.php -> website.com/index.php?com=detail&catname=books&prodname=java
RewriteRule ^/([^/]+)/([^/]+).php$ /index.php?com=detail&catname=$1prodname=$2 [L]
查看更多
登录 后发表回答