Url rewrite : I can't open urls that have hyphens

Hi,

I’m using the rule below, but there’s a problem. I get a 404 error if there’s a hyphen in the url. What could be the reason for that?

http://localhost/product/1/tag/works
http://localhost/product/1/tag/doesnt-work

RewriteRule ^product/(\w+)/tag/(\w+)/?$ main/p/tag.php?product_id=$1&tag_value=$2

A hyphen is not a “word” character
http://php.net/manual/en/regexp.reference.escape.php

A “word” character is any letter or digit or the underscore character, that is, any character which can be part of a Perl “word”. The definition of letters and digits is controlled by PCRE’s character tables, and may vary if locale-specific matching is taking place. For example, in the “fr” (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w.

2 Likes

Ah, you’re right.

using (.*) solved the problem.

And can cause other problems

I would not use the anything - everything - nothing there. I would use
([\w-]+)

1 Like

I’d vote for [^/]+ as the most future proof. It matches a path segment – that is, anything between the slashes.

1 Like

For the OP’s stated problem, Alan (Mittineague) has the “correct” answer.

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.