views:

596

answers:

2

I wanted to set .htaccess to rewrite:

example.com/bla/info/ to example.com/info/index.php?q=bla + example.com/bla/info/txt/ to example.com/info/index.php?q=bla&t=txt

I wanted the browser still to display: example.com/bla/info/txt/

I didnt want rewrite in othe level 2 rewrites. like: example.com/bla/xxx/ or example.com/ccc/zzz/aaa/

but example.com/silly/info/ would work as well as example.com/strange/info/mytxt/

did this make sense?

Any help?

A: 

If you start your pattern with ^ and end it with $, the rule will only apply if the whole string matches.

RewriteRule   ^/bla/info/$      /info/index.php?q=bla
RewriteRule   ^/bla/info/txt/$  /info/index.php?q=bla&t=txt

If you use do not use the [R] option, the URL shown in the browser will be whatever the user entered.

Are you trying to make this general-purpose? If so, you can use regex-type matching and variable substitution. To make 'bla' match any string up to the next slash (/), use

([^/]+)

in your pattern and reference it with $1 in your substitution.

RewriteRule ^/([^/]+)/info/$          /info/index.php?q=$1
RewriteRule ^/([^/]+)/info/([^/]+)/$  /info/index.php?q=$1&t=$2

I recommend the Apache web pages about mod_rewrite for more information.

[Edit. Fixed the rules to not include the host name in the pattern, as the original poster figured out.]

--
bmb

bmb
A: 

you got me in the right track.

i ended up with something like this:

RewriteRule ^([^/\.]+)/info/?$ example/info/index.php?q=$1 [L]

thanks a lot

Hugo Gameiro
Oops, I forgot the hostname doesn't appear in the pattern. I fixed the answer.
bmb