Forum Moderators: phranque

Message Too Old, No Replies

Problem with rewritecond or rewriterule?

Redirection doesn't work based on query_string

         

ZviJ

4:00 am on Jun 12, 2005 (gmt 0)

10+ Year Member



Hi All,

The goal is to redirect certain user galleries from one site to another. Gallery is specifyed in query string as index.php?cat=nn where nn is gallery number.

So, I have:

RewriteCond %{QUERY_STRING} ^cat=24$ [NC]
RewriteRule ^.*index\.php(\?.*)$ http://example.com/cpg/index.php$1 [R=301,L]

I've tried several variants of rewriterule including

RewriteRule ^.*index\.php(\?.*)$ http://example.com/cpg/index.php? [R=301,L]

Nothing worked so far. No redirection occurs, no error is reported either :(

[edited by: jdMorgan at 9:13 pm (utc) on June 12, 2005]
[edit reason] Example.com [/edit]

jd01

9:34 am on Jun 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ZviJ,

There are three things I see:
1. (\?.*) checks for a literal?, which is what says 'query string' to Apache, so Apache does not see that as part of the URL.

2. Is more efficiency than anything else: beginning a line with .* (or anyother 'catch all' is wholy inefficient. Unfortunately, I do not know where the index.php file is, but there are a couple of things you can do:

First, put your .htaccess in the directory of the actual file, then remove the 'catch all' from the rule, so if the index was in the directory foo, you could put your .htaccess in the /foo/ directory also, and shorten the rule:
RewriteRule ^index\.php$

Second, you could use the actual path the /foo/ directory:
RewriteRule ^foo/index\.php$

Third, you could use a 'forward looking' expression, but without knowing your directory structure, this is tough to recommend.

3. With the correction of number one, you are passing a blank variable. To pass the correct variable, you will need to create a variable in the condition using () and then reference it in the rule with %1. You will also need to add the? that is not really 'counted' or 'stored' anyhwere, to the rule. (Note, you *may* be able to use only Query String Append, if you are only passing the original query string back to the rule.)

RewriteCond %{QUERY_STRING} ^(cat=24)$ [NC]
RewriteRule ^index\.php$ http://example.com/cpg/index.php?%1 [R=301,QSA,L]

This as it is written, is for the actual directory the index.php file resides in. If you need to have your .htaccess in the root, but the actual index.php you are redirecting is in a deeped directory, use the second example and add the necessary paths. (Keep in mind the preceding / is stripped in the .htaccess file, where it is necessary in the httpd.conf file.)

Hope this helps.

Justin

[edited by: jdMorgan at 9:14 pm (utc) on June 12, 2005]
[edit reason] Example.com [/edit]

ZviJ

10:07 pm on Jun 12, 2005 (gmt 0)

10+ Year Member



Thanks for help...
Though it's the same result for whatever reason.
I put .htaccess file in the same directory as index.php

so the lines look like this:


RewriteCond %{QUERY_STRING} ^(cat=24)$ [NC]
RewriteRule ^index\.php$ http://example.com/cpg14/index.php?cat=24 [R=301,QSA,L]
Alternatively:
http://example.com/cpg14/index.php?%1

No redirection occures in both cases, no error is reported either. Really weird..

[edited by: jdMorgan at 10:57 pm (utc) on June 12, 2005]
[edit reason] example.com [/edit]

jdMorgan

11:01 pm on Jun 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try a simple test:

RewriteRule ^foo\.php$ http://example.com/cpg14/index.php?cat=24 [R=301,L]

Now request "foo.php" from the diredctory where this .htaccess code is located using your browser, and see if you get redirected to "/cpg14/index.php?cat=24"

This eliminates one layer of complexity, and will let us determine if mod_rewrite works at all for you.

Jim

ZviJ

11:33 pm on Jun 12, 2005 (gmt 0)

10+ Year Member



Tried with and w/o rewritecond... Doesn't work and no errors again.

I ran apache on my local comp and tried to debug the issue. Not mcuh success. Redirection from rewriterule doesn't work on my local setup either, so it's not my hosts config.

I enabled logging in httpd.conf


RewriteEngine on
RewriteLog "rewrite.log"
RewriteLogLevel 9

then I have .htaccess in that directorywhere index.php resides:


RewriteEngine on

RewriteRule ^index\.php$ http://example.com/cpg14/index.php?cat=24 [R=301,L]

No action from rewrite engine whatsoever. Stays on the same url. I thought may be the problem was that redirection was going out to another site, I know that shouldn't be a problem, but alas, local redirection won't work either.
rewrite engine apprently works, because it's logging all these requests...

Here's what's being logged:


127.0.0.1 - - [12/Jun/2005:16:29:37 --0700] [localhost/sid#26bdb0][rid#61d2c0/initial] (2) init rewrite engine with requested uri /cpg14/index.php
127.0.0.1 - - [12/Jun/2005:16:29:37 --0700] [localhost/sid#26bdb0][rid#61d2c0/initial] (1) pass through /cpg14/index.php

At the same time redirect and redirectmatch work fine.
Also, I am able to block hotlinks on images from rewrite engine... So it's definitely on. Just something stupid with this rule :(

ZviJ

11:59 pm on Jun 12, 2005 (gmt 0)

10+ Year Member



Solved!
Doh :) It was real stupid of me I guess. I donno how, but apparently I managed to put rewrite engine off in root .htaccess file. After removing that part all works fine.
Thanks everyone for your help. Couple more questoins..

Finally I ended up with this:


RewriteEngine on

RewriteCond %{QUERY_STRING} ^(cat=24¦cat=2¦cat=9¦cat=16¦cat=3¦cat=20¦cat=22)$ [NC]
RewriteRule ^index\.php$ http://example.com/cpg14/index.php [R=301,L]

1) if I have R=301, QSA,L it works same. Is QSA by default on?
2) If I append %1 to index.php as in prev. post then I get wrong string - http://example.com/cpg14/index.phpcat=22?cat=22
And this happens with or w/o QSA.
As far as I understand this is because rewrite engine is replacing index.php in original url with http://example.com/cpg14/index.php as in the rewrite rule. So, %1 appends found results to substitute string first and then the result gets inserted instead of index.php so that?cat=22 is from the original reuqest?
For my particular case this solution works, but what if I wanted to replace query string, or alter it?
Let's say I need to replace cat=22 I need cat=142?

jdMorgan

1:34 am on Jun 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{QUERY_STRING} ^cat=(2¦3¦9¦16¦2[024])$ [NC]
RewriteRule ^index\.php$ http://example.com/cpg14/index.php? [R=301,L]

If you want to Append a new query part from the RewriteRule to the existing query string, then you use [QSA] (Query String Append). If you want to replace the query string, then do not use [QSA].

If you create a back-reference in the QUERY_STRING RewriteCond, and include that (by using %1, for example) in your RewriteRule substitution, then that's going to include the back-referenced query (sub)string.

The "?" in the rule above deletes any incoming query string after RewriteCond has tested it. Since there is no QSA flag, the query string is removed.

Jim

ZviJ

1:37 am on Jun 13, 2005 (gmt 0)

10+ Year Member



Thanks!
I was about to edit my post. After experimenting more with rewrite rules I figured out? was the key :)
Thanks guys, it was a good lesson learning wise.