views:

67

answers:

1

Hello fellow programmers,

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.

+3  A: 

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]
Gumbo
Sidenote: If they're in a per-directory (`.htaccess`) context, the `RewriteRule` test patterns should lose the initial forward slash. It worked perfectly on my test server using the example in the question, +1
Tim Stone
@Tim: Thanks for the testing.
Gumbo
I edited the question.. your rules are almost there.
fampinheiro