PDA

View Full Version : Htaccess - RewriteRule - Url with Arguments


Jerome
08-22-2005, 04:04 PM
Hi,

I want to replace:

http://www.site.com/index.shtml?red

with:

http://www.site.com/?widget=red

I tried (engine is on etc):

RewriteRule ^index\.shtml?red$ http://www.site.com/?widget=red [r=301,l]

and

RewriteRule ^index\.shtml\?red$ http://www.site.com/?widget=red [r=301,l]

Without effect, however this:

RewriteRule ^index\.shtml$ http://www.site.com/?widget=red [r=301,l]

Works.

What do I do wrong?
Thanks,
Jerome

schleppel
08-22-2005, 06:34 PM
You could do a few things depending on your needs.

1) if you want any query string added, you can use [QSA] (Query String Append)
RewriteEngine On
RewriteRule ^index\.shtml$ http://someserver.tld/something.php [R=301,QSA,L]

2) if you want only to redirect if the value is red, you would do this
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(red)$
RewriteRule ^index\.shtml$ http://someserver.tld/something.php?%1 [R=301,L]

3) if you want only to redirect one of a set of values (red or blue, split by a pipe) you could do this
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(red|blue)$
RewriteRule ^index\.shtml$ http://someserver.tld/something.php?%1 [R=301,L]

Edit, if it's the same domain you don't need it, just /something.php
Edit2: I said [QSA] was optional on the last 2, it's not (although it could be).

Jerome
08-22-2005, 07:22 PM
Hi schleppel,

Option 2 is the best solution I think, but there is something going wrong
In my case it should look like:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(red)$
RewriteRule ^index\.shtml$ http://someserver.tld/?widget=red%1 [R=301,L]

In the title I see quickly:
http://someserver.tld/?widget=red&red

Which is already not ok (&red to much)

But what I feel very strange is that the url in the address-bar doesn't change, is this normal? It's a redirect not?

Thanks jerome

schleppel
08-22-2005, 08:06 PM
On my test server this works fine.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(red)$
RewriteRule ^index\.shtml$ http://82.36.227.98/?widget=%1 [R=301,L]

Your &red problem was that %1 holds the value of red, you don't need it if your hardcoding it (it's used for multiple ones), but it's better if you don't, as then you only need to change one value.

I have no idea why it doesn't redirect.