Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 4.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-22-2005, 03:04 PM   PM User | #1
Jerome
Regular Coder

 
Join Date: Oct 2002
Posts: 299
Thanks: 0
Thanked 0 Times in 0 Posts
Jerome is an unknown quantity at this point
Htaccess - RewriteRule - Url with Arguments

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
Jerome is offline   Reply With Quote
Old 08-22-2005, 05:34 PM   PM User | #2
schleppel
Regular Coder

 
Join Date: Oct 2004
Posts: 330
Thanks: 0
Thanked 13 Times in 13 Posts
schleppel is an unknown quantity at this point
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)
Code:
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
Code:
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
Code:
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).

Last edited by schleppel; 08-22-2005 at 05:43 PM..
schleppel is offline   Reply With Quote
Old 08-22-2005, 06:22 PM   PM User | #3
Jerome
Regular Coder

 
Join Date: Oct 2002
Posts: 299
Thanks: 0
Thanked 0 Times in 0 Posts
Jerome is an unknown quantity at this point
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
Jerome is offline   Reply With Quote
Old 08-22-2005, 07:06 PM   PM User | #4
schleppel
Regular Coder

 
Join Date: Oct 2004
Posts: 330
Thanks: 0
Thanked 13 Times in 13 Posts
schleppel is an unknown quantity at this point
On my test server this works fine.
Code:
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.
schleppel is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:23 PM.


Advertisement
Log in to turn off these ads.