views:

68

answers:

2

How could I use a rewrite to change:

/?tag=foo

To:

/tag/foo

I tried:

RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/$1 [L]

But it did not work.

+4  A: 

Try the following:

RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/%1 [L]

Usually, rewrites are used to achieve the opposite effect. Are you sure you don't really want to do the following?

RewriteRule ^tag/(.+)$ index.php?tag=$1 [L]
Andrew Moore
+2  A: 

To avoid recursion, you should check the request line instead as the query string in %{QUERY_STRING} may already have been changed by another rule:

RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)tag=([^&\s]+)&?([^\s]*)
RewriteRule ^(.*)$ /tag/%3?%1%4 [L,R=301]

Then you can rewrite that requests back internally without conflicts:

RewriteRule ^tag/(.*) index.php?tag=$1 [L]
Gumbo
Andrew Moore and GumboThank you, but thank you very much.I'm trying to a solution to more than one week without success.The solution presented by Gumbo resolve the problem.When meeting people willing to help others who do not know I am sure that we can have a better world.Sorry errors, but my knowledge of English is very limited.I just want to thank.Vera
Vera
@Vera: Got your message :)
Gumbo