2

I have a structure of my web http://example.com/category_name/product_name. I rewrite it to PHP by using the following RewriteRule :

RewriteRule ^(.*)/(.*)/$ detail-produktu.php?kategorie=$1&nazev=$2

But the problem occurs when I am trying to rewrite the address http://example.com/user/david. This URL Always starts with /user, so I want to rewrite it like this:

RewriteRule ^user/(.*)/$      index.php?user=$1

But still the URL with /user goes to detail-produktu.php.

What can I do?

1 Answer 1

1

You have a few potential problems.

  1. The URL you have stated (http://example.com/user/david) does not end with a slash, but the RewriteRule pattern includes a trailing slash, so it will not match.

  2. You will need to include the L flag to stop the rewriting process (at least the current round of processing in .htaccess), otherwise the request is likely to be caught by the other rule as well.

  3. The order matters. The most specific rules need to be first. So, the rule to catch the /user request must be first.

For example:

RewriteRule ^user/([^/]+)$ index.php?user=$1 [L]
RewriteRule ^([^/]+)/(.*)$ detail-produktu.php?kategorie=$1&nazev=$2 [L]

I've removed the trailing slash from the pattern. Add this back if the trailing slash is required. I've also made the pattern a bit more restrictive, so that it matches whole path segements, rather than anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.