Mod Rewrite Dash problem - Anyone to help with this challenge?

Long dynamic url:$article_id&category=$category&status=$status&title=$title

Rewrite rule(post is just a fake page name):

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

Generated static url:$article_id-$category-$status-$title.htm

The problem is that since this rule uses 3 dashes,any variable with an extra dash in its value will break the rule. for example,if the title is How-to-remove-stains,the url will look like:

The title value will be assumed as an extenstion of the the Category(Home/Garden) or Status(Active) value.

Can the last variable value($title) have any number of dashes without confusing apache and breaking the rule?
If I use slashes instead,I will have the same problem since i have category names with slashes.

Thank you very much for any help

Ha, you fell in the trap of the infamous :redhot: (.*) :redhot: atom!

It will eat anything and everything and that’s the thing that’s screwing the rule up.
Generally what you ask is not possible, but if you are sure that there can be only be multiple dashes in the last part and never (ever!) in any of the other parts, there is a solution.

Your regex is:

The first (.*) is for the article_id. From what I understand this is a number, so I’d replace that with (\d+) to match one or more digits.

The second and third (.) are for category and active respectively and the problem is that the supergreedy (.)'s are eating up the - that belong the title – the last atom.
To prevent these atoms from eating up the dashes, change them to ([^-]+) – match one or more of any character except for dashes.

That way the dashes are left for the last atom, and everything should work as you want it

Lastly I would replace the last (.*) with (.+) to ensure there is at least one character and disabling the possibility of no characters at all.


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

Does that make sense?

Thank you very much man for investing your time and thought to help me out with this!
I sincerely appreciate it.
Yes,your explanation makes perfect sense - It’s a great tutorial in itself on url rewrite
I just tested and worked like a charm. It also gave me helful insights to fix other rules.
Time to check your portfolio and add your contact :slight_smile:
Thanks a lot again!