Forum Moderators: phranque
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]
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]
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]
RewriteRule ^foo\.php$ http://example.com/cpg14/index.php?cat=24 [R=301,L]
This eliminates one layer of complexity, and will let us determine if mod_rewrite works at all for you.
Jim
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 onRewriteRule ^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 Finally I ended up with this:
RewriteEngine onRewriteCond %{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?
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 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