1

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 Answer 1

3

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanx AndiDog! I've removed the "L" mark and it seems to work (except for "catname" value, it returns only "prodname"). Besides can't understand how should be the regex to match the absolute urls...
@Luciano: I added an example to show the use of absolute URLs. This way, the rules are unambiguous. Either the first or the second will be chosen (or none).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.