Reputation: 1134
I have the following rules in my .htaccess
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
# single product page
RewriteRule ^uleiuri/(.*)/(.*)/(.*)-([0-9]+)/?$ produs.php?id=$4 [L]
# oils page by category and brand
RewriteRule ^uleiuri/(.*)/(.*)/?$ uleiuri.php?cat=$1&brand=$2 [L]
# oils page by category
RewriteRule ^uleiuri/(.*)/?$ uleiuri.php?cat=$1 [L]
</IfModule>
What it does: website.com/uleiuri
shows all the products, website.com/uleiuri/category-name
show all the products for a certain category, website.com/uleiuri/category-name/brand-name
, show all the products for a certain category and also for a certain brand, and finally website.com/uleiuri/category-name/brand-name/name-of-the-product-x
is the product page.
As it is now it works, but if I add a /
at the end of any of them, the rules fail and it show me all the products, so for example website.com/uleiuri/category-name/brand-name/
returns all the products.
I hope the problem is clear and I thank you for your help.
Upvotes: 0
Views: 40
Reputation: 827
You should avoid matching with *
and use +
instead. What more, your rules are "greedy" and that's why to much is matched. You should replace (.*)
with ([^/]+)
which will match only strings to /
character and at least one character.
When user enters adress: website.com/uleiuri/category-name/brand-name/
using your rules, cat
variable is filled with category-name/brand-name
and brand
is an empty string, and that's probably why all the products are returned.
Your rules should look like those:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
# single product page
RewriteRule ^uleiuri/([^/]+)/([^/]+)/([^/]+)-([0-9]+)/?$ produs.php?id=$4 [L]
# oils page by category and brand
RewriteRule ^uleiuri/([^/]+)/([^/]+)/?$ uleiuri.php?cat=$1&brand=$2 [L]
# oils page by category
RewriteRule ^uleiuri/([^/]+)/?$ uleiuri.php?cat=$1 [L]
</IfModule>
Upvotes: 1