Forum Moderators: phranque

Message Too Old, No Replies

htaccess mod_rewrite pattern recognition problem?

Getting 500 and 404 errors

         

GT500_Grad

2:27 am on Dec 13, 2009 (gmt 0)

10+ Year Member



I'm implementing twitter to my site and since I'm using codeigniters clean urls, I need to redirect the twitter callback url to a method url. Below is the url that twitter is calling back.

...twitter_demo/dummy?oauth_token=BGvrWnAWkA5uTvZViC7CERbBLKGRvZD1oiPPo6BDOA

Below is the Rewrite that I have in the htaccess.
RewriteRule ^twitter_demo/dummy?oauth_token=(.*+)$ twitter_demo/dummy/$1 [NC,L]

I need to pull out the oauth_token and set it to the $1 var. Do you see anything wrong with the syntax? It seems to be having a hard time finding the pattern and setting it. I've tried a multiple different things like, (.*), (.+), (.*+), but none of them seem to work on the string.

Any ideas?

g1smd

11:53 am on Dec 13, 2009 (gmt 0)

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



.* = anything or nothing.

.+ = something.

.*+ = ambiguous, I wouldn't use it.

The main problem is that RewriteRule cannot see query string data.

You need a separate, preceding, RewriteCond to look at the QUERY_STRING value.

This is something covered in the tutorials linked from the forum charter.

Additionally, you use the word 'redirect' in your question and provide code for a 'rewrite' below that. A 'rewrite' is the correct thing to do, but you must be very careful with the terminology. A 'redirect' would be a disaster.

GT500_Grad

3:03 pm on Dec 14, 2009 (gmt 0)

10+ Year Member



Thanks for getting back to me, and I will try to keep the terminology more clear.

I added the RewriteCond as below,

RewriteCond %{QUERY_STRING} ^oauth_token=(.*)$
RewriteRule ^twitter_demo/dummy /twitter_demo/dummy/%1 [L]

But now it is throwing a 500 internal server error. Could this be due to the RewriteRule reading and writing to the the same dummy method?

If I change the line to,

RewriteRule ^twitter_demo/status /twitter_demo/dummy/%1 [L]

It gives me a 404 error, stating that the page is not there, yet I know it is.

jdMorgan

3:36 pm on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The most likely reason your original code looped is that the output path matches the input pattern. This is likely due to the facts that the input pattern is not end-anchored and you did not clear the query string in your substitution path (by adding a "?" to it as described in the mod_rewrite documentation).

So your pattern will match "twitter_demo/dummy" followed by anything or nothing, because it's not anchored. Also, the original query string will still be present on the rewritten path, so it will match too.

In addition, should additional query parameters be present (by error or malicious intent) in the request, your query string pattern will fail.

All-in-all, based on just what's here in the thread, I'd suggest:


RewriteCond %{QUERY_STRING} ^([^&]*&)*oauth_token=([^&]*)
RewriteRule ^twitter_demo/dummy$ /twitter_demo/dummy/%2? [L]

But in order to further debug code if there are remaining problems, we'll need to see the current code, the input URL, and the expected output filepath. So if that doesn't work, please provide an example URL and the local server path to the existing file to which the URL should resolve. And if any changes are made to the code, we'll need to see them, too.

Jim

GT500_Grad

3:58 pm on Dec 14, 2009 (gmt 0)

10+ Year Member



That worked Thanks!

I was afraid to put the end-anchor because I didn't think it would recognize the url because of the string still being present. I missed the ? to clear it. Thanks again!

GT500_Grad

7:59 pm on Dec 18, 2009 (gmt 0)

10+ Year Member



If I wanted to read in a second variable now, all I should have to do is add that pattern to the RewriteCond, correct?

from:
RewriteCond %{QUERY_STRING} ^([^&]*&)*oauth_token=([^&]*)

to this:
RewriteCond %{QUERY_STRING} ^([^&]*&)*oauth_token=([^&]*)&next=([^&]*)

correct?

[edited by: jdMorgan at 1:26 am (utc) on Dec. 19, 2009]
[edit reason] Disabled smilies in code. [/edit]

jdMorgan

1:27 am on Dec 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but it'll likely be faster to test simple stuff like this, instead of waiting around all day for our opinions here... :)

Jim

GT500_Grad

6:10 pm on Dec 21, 2009 (gmt 0)

10+ Year Member



Thanks for getting back to me.

I actually had tried it, along with,
RewriteCond %{QUERY_STRING} ^([^&]*&)*oauth_token=([^&]*)\&next=([^&]*)

and neither of them seemed to have worked. But I just realized the error was something else.

[edited by: jdMorgan at 10:55 pm (utc) on Dec. 21, 2009]
[edit reason] Disabled smilies in code. [/edit]