Filipe Pinheiro
Filipe Pinheiro

Reputation: 1102

Apache Rewrite Rule (Regex)

I'm using apache and i need to rewrite URL of type:

/a/b/c/d/boundary/stuff*

to:

/boundary/a_b_c_d/stuff*

I need to rewrite the first Uri to the second format.

The number of elements before '/boundary' are variable and i want to replace all slashes('/') between elements by '_'

The boundary word is always the same.

I believe i need to do make two rules, one for slash replace and another to change boundary to the beginning of the URL:

The second rule i think is something like ^/(.*?)/\bboundary\b to /boundary/$1

Is it possible to achieve what i want ?!

How?!

Thank you.

EDIT

I want to match until first boundary word, it's possible to have a URL like

/a/b/c/d/boundary/boundary

EDIT

Gumbo thank you for your help

based on your rewrite rules i managed to create one.

Upvotes: 0

Views: 2098

Answers (1)

Gumbo
Gumbo

Reputation: 655239

Try these rules:

RewriteRule ^/([^/]+)/(.+)/boundary/([^/]+)$ /$1_$2/boundary/$3 [N]
RewriteRule ^/([^/]+)/boundary/([^/]+)$ /boundary/$1/$2 [L]

Be careful with the first rules with the N flag causes an internal rewrite without incrementing the internal counter. So chances are that you have infinite recursion.


Edit    After you’ve changed your question:

RewriteCond $1 !=boundary
RewriteCond $2 !=boundary
RewriteRule ^/([^/]+)/([^/]+)(/.*)?$ /$1_$2$3 [N]
RewriteRule ^/([^/]+)/boundary(/.*)$ /boundary/$1$2 [L]

Upvotes: 3

Related Questions