Forum Moderators: phranque

Message Too Old, No Replies

htaccess query string add query

         

twofish

10:31 am on Aug 14, 2010 (gmt 0)

10+ Year Member



Sorry to ask a simple question. To be honest my head is hurting from reading about htaccess....

I have urls like this:

http://www.example.com/?cat=0
http://www.example.com/?dog=0

All I want to do is add a query to the string if cat is specifies in the string so:

http://www.example.com/?cat=0
becomes
http://www.example.com/?cat=0&rat=0

and

http://www.example.com/?cat=0&dog=3
becomes
http://www.example.com/?cat=0&dog=3&rat=0

(rat will always be 0)

But

http://www.example.com/?dog=0
remains unchanged.

Thanks

jdMorgan

12:04 pm on Aug 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your best-effort coding attempt as a basis for discussion.

You can do a site search here for previous examples of "query string append" as well.

Thanks,
Jim

twofish

2:00 pm on Aug 14, 2010 (gmt 0)

10+ Year Member



# replace string
RewriteCond %{QUERY_STRING} cat=([^&]*)
RewriteRule ^(.*)$ http://www.domain.com/index.html?rat=0&cat2=%1 [NC]


Which kind of works but as you can see there are problems. If I don't change cat to cat2 then it loops. I'd like the rule to read:

RewriteRule ^(.*)$ http://www.domain.com/index.html?rat=0&cat=%1 [NC]


But that loops. If I use QSA I don't see how I can use that condition and not cause a loop because I want cat to stay in the string. Although QSA is obviously preferable as this depends on cat=0 being the first item in the query string.

Thanks so much for your attention.

twofish

8:42 pm on Aug 14, 2010 (gmt 0)

10+ Year Member



OK this is my next best effort.

# replace string
RewriteCond %{QUERY_STRING} cat=([^&]*)
RewriteCond %{QUERY_STRING} !rat=([^&]*)
RewriteRule ^(.*)$ http://www.niccol.info?rat=0 [QSA]


Which seems to work. Is there a better way of doing it?

g1smd

9:05 am on Aug 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




If that query string only appears with certain file names, then change the (.*) pattern to match that filename. At present, the above code looks for a query string on every request for every page, every image, every CSS file, etc.



The above code generates a 302 redirect. Specify R=301 if you need a 301 redirect.


Add the L flag to every rule unless you know exactly why you are omitting it.

twofish

9:57 am on Aug 15, 2010 (gmt 0)

10+ Year Member



Thanks, that all makes sense even though I am just finding my way around htaccess.

Is what you are saying that if the rule does not apply then that is checked first before the conditions? I am uncertain about the order that this is actioned. It is not in the order written?

It is difficult to exclude by file name but I guess I could exclude all the directories that contain css, images etc. Is that a good way to go?


If that query string only appears with certain file names, then change the (.*) pattern to match that filename. At present, the above code looks for a query string on every request for every page, every image, every CSS file, etc.



The above code generates a 302 redirect. Specify R=301 if you need a 301 redirect.


Add the L flag to every rule unless you know exactly why you are omitting it.

jdMorgan

1:24 pm on Aug 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the Apache mod_rewrite documentation at apache.org for information about the rule processing sequence and its effects (allowing full back-referencing, among others).

Jim

g1smd

7:21 pm on Aug 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The RewriteRule pattern is the very first thing to be evaluated in the sequence - even before the RewriteCond.

Until I realised that, I was writing very inefficient code - and it took me years to figure that out.

twofish

8:10 pm on Aug 15, 2010 (gmt 0)

10+ Year Member



Thanks guys.

Yes, that order seems pretty odd. But now I know it is going to help a huge amount.

As I say thanks for your time. It is greatly appreciated.