Mod Rewrite Dash problem

Here is the solution which I found in this topic:

My problem is that variables includes words separated with dashes, for example:
in link$article_id&category=$category_name&status=$status_name&title=$title

where : $category = blue-machine AND $status = new-user AND $title = buy-promo-action

in proposed solution :

Options +FollowSymLinks
RewriteEngine on
RewriteRule post(\d+)-([^-]+)-([^-]+)-(.+)\.htm$ /Articles/details.php?article_id=$1&category=$2&status=$3&title=$4

Rewrite rule breaks if variables $2 OR $3 includes DASHES !

Need help to sort out this issue. Thanks.

It sounds like you want each piece of information (id, category, status, title) to be separated by hypens, while at the same time allowing each segment to itself contain hyphens. So, for example, if the URL is, /blue-machine-new-user, does that mean the category is “blue” and the status is “machine-new-user”? Or maybe the category is “blue-machine-new” and the status is “user”?

Unfortunately there’s no easy answer here. You’ve designed your URLs in such a way that it’s impossible to tell where one piece of information ends and the next begins. I don’t think there’s any rewrite rule that will save you here. You simply need to rethink your URLs.

The person in that other thread was very nearly in the same pickle. The only thing that saved him is that he only allowed hyphens in the last segment.


The problem that I see is not with -'s in your {REQUEST_URI} but the format you’re using to generate the URI THEN what are you using to access your database, the article_id OR the category, status and title (you only need one)?

It looks like your URI is in the format post{digits}-{string}-{string}-{garbage}.htm. Since you are including the article_id, there is no need to capture anything about the category, status or title so all that’s needed in your RewriteRule’s regex is


Note the start anchor and NOTHING after the digits is required. Of course, the target would be Articles/details.php?article_id=$1.

If, for some strange reason, your details.php requires the other information, you will need to change the artificial divider you’re using between your values (so you don’t split a value). Personally, I only use the title ( for a client’s website and it’s worked a treat for years. Note that, because I don’t need a separator, I have enabled my client to use -'s in his titles (there are characters which he must ardently avoid, though!)…

CAUTION: Using “garbage collectors” like .* and [!-]+ is dangerous as you never know what you’ll be capturing and sending to your database. I prefer to use regex like [-a-zA-Z]+ where you have your not-dash code simply to avoid having to test for garbage.

Have a look at the tutorial I wrote for SitePoint members several years ago ( It’s helped many members and should benefit your understanding, too.



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