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


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?


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

A hyphen is not a “word” character

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.


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

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.



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