scientist.rahul
scientist.rahul

Reputation: 157

Recursive Regular Expression for mod rewrite

I'm trying to use mod_rewrite to recursivly modify my web page urls, but am not able to figure out rewrite rule for this situation:

I want to change my URLs from pattern

http://mywebsite.com/token1-token2-token3-token4-categoryname

to

http://mywebsite.com/?cat=categoryname&search=token1+token2+token3+token4

Challenge is that the number of tokens in url may vary, so I need to somehow handle token matching and conversion recursively. Any ideas how can this be achieved?

Upvotes: 1

Views: 125

Answers (2)

Jon Lin
Jon Lin

Reputation: 143886

Try:

# Remove all of the - and replace with +
RewriteRule ^(.*)-(.*)$ /$1+$2 [L]

# only rewrite when there are no more -
RewriteCond %{REQUEST_URI} !\-
RewriteRule ^(.+)\+([^\+]+)$ /?cat=$2&search=$1 [QSA,L]

Upvotes: 1

Ωmega
Ωmega

Reputation: 43673

RewriteRule ^/?(.+)\-([^\-]+)$ /?cat=$2&search=$1 [QSA,L]

Upvotes: 0

Related Questions