.htaccess: Problem with URL rewriting

有些话、适合烂在心里 提交于 2019-12-11 11:38:27

问题


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:


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]


来源:https://stackoverflow.com/questions/3230795/htaccess-problem-with-url-rewriting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!