Forum Moderators: phranque

Message Too Old, No Replies

One last question

         

CWebguy

5:26 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



Ok, I'm going to have to pick your brain one more time ;) Thanks to all the moderators on here for all your help. Really, thank you.

My final question (of the day) is...

I can do the 301 redirect, I can do the URL rewrite, but I'm having trouble putting them together.

Current code=


RewriteCond %{QUERY_STRING} ^ca=([0-9]+)
RewriteRule ^subdir/$ http://www.example.com/subdir/%1? [R=301,L]
RewriteRule ^subdir/([0-9]+) /subdir/?ca=$1 [L]

So the rewrite rule by itself seems to work, as does the redirect, but put them together and it doesn't. Any thoughts?

Much abliged!
CWebguy

[edited by: CWebguy at 5:27 pm (utc) on Mar. 10, 2009]

CWebguy

5:34 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



Oh wait, found this link [webmasterworld.com...] Will read and let you know if I have any more questions. Thanks.

g1smd

6:43 pm on Mar 10, 2009 (gmt 0)

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



The redirect should only fire for DIRECT CLIENT REQUESTS not as a result of the internal pointer being updated during a rewrite.

That is, you need to look at THE_REQUEST when evaluating whether the redirect needs to be fired.

jdMorgan

9:56 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Externally redirect only direct client requests for subdir+query-string URL back to static URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /subdir/\?ca=([0-9]+)\ HTTP/
RewriteRule ^subdir/$ http://www.example.com/subdir/%1? [R=301,L]
#
# Internally rewrite requests for static URL to query string filepath
RewriteRule ^subdir/([0-9]+) /subdir/?ca=$1 [L]

Jim
[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 7:23 pm (utc) on Mar. 11, 2009]

CWebguy

3:30 am on Mar 11, 2009 (gmt 0)

10+ Year Member



sorry to say but the code isn't working.

http://www.example.com/subdir/?ca=123

still doesn't redirect even after adding the {THE_REQUEST} code.

thanks.

[edited by: CWebguy at 3:34 am (utc) on Mar. 11, 2009]

g1smd

9:47 am on Mar 11, 2009 (gmt 0)

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



Is this code placed in the root folder, or the subdir folder?

Did you flush the browser cache before testing?

CWebguy

4:17 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



In the root folder, yes I did.

Final code is:


RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /store/?ca=([0-9]+)\ HTTP/
RewriteRule ^store/$ http://www.example.com/store/%1? [R=301,L]
RewriteRule ^store/([0-9]+)$ /store/?ca=$1 [L]
#RewriteRule ^store/([0-9]+)/([a-zA-Z0-9]+)$ store/it.php?ca=$1&as=$2 [L]
#RewriteRule ^store/([0-9]+)/pg-([0-9]+)$ /store/?ca=$1&pg=$2 [L]

Trying to redirect : www.example.com/store/?ca=123 to
www.example.com/store/123 which also calls example.com/store/?ca=123 as the script. I commented out the last two lines as this isn't the focus now.

Thanks.

[edited by: CWebguy at 4:29 pm (utc) on Mar. 11, 2009]

jdMorgan

7:22 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The literal question mark in the RewriteCond pattern needs to be escaped as shown, otherwise it will be taken as a regular-expressions quantifier meaning, "zero or one of the preceding character."

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sto[b]re/\?ca[/b]=([0-9]+)\ HTTP/
RewriteRule ^store/$ http://www.example.com/store/%1? [R=301,L]

Jim

CWebguy

7:59 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



The literal question mark in the RewriteCond pattern needs to be escaped as shown, otherwise it will be taken as a regular-expressions quantifier meaning, "zero or one of the preceding character."

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /store/\?ca=([0-9]+)\ HTTP/
RewriteRule ^store/$ http://www.example.com/store/%1? [R=301,L]


Haha, yes! works now. Now I have to figure out how to understand it. So I'm guessing everything was correct as far as placement? (redirects before rewrites,etc.? i'm learnin')

Thanks a million.

CWebguy

[edited by: CWebguy at 8:02 pm (utc) on Mar. 11, 2009]

g1smd

8:02 pm on Mar 11, 2009 (gmt 0)

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



Lines 3 and 4 MUST be moved to be the very first two lines. Specific redirect before General redirect.

Other than as noted above, I don't see why it wouldn't work. Ah, you fixed it while I was still reading.

You do have

RewriteEngine On
ahead of all of this though?

CWebguy

8:12 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



yes, of course, sorry, but not optsymlink thing. Thanks for the help.

g1smd

8:51 pm on Mar 11, 2009 (gmt 0)

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



Lines 3 and 4 MUST be moved to be the very first two lines. Specific redirect before General redirect.

Are you on that one?

CWebguy

9:14 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



Yes, I'll switch it, the {THE_REQUEST} before the non-www to www rewrite. Gotcha, thanks again.

g1smd

9:46 pm on Mar 11, 2009 (gmt 0)

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



You need to do that for one reason.

Think what happens for a non-www request with the ?ca parameter included.

For that, there will be a double redirect: first the non-www to www redirect, and then the parameter to static redirect kicks in. That Redirection Chain is a BAD thing.

Changing the order of the code around fixes that. With the parameter redirect first, any non-www request with the ?ca parameter included is redirected to static format and the www is fixed at the same time.

That is, the following non-www to www redirect kicks in only for all other requests.