Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule to redirect the whole URI substring

RewriteRule, QSA

         

mikeip

1:39 am on Nov 22, 2008 (gmt 0)

10+ Year Member



Hi,

Seems like a easy question but I am not nearly an expert in Apache/mod_rewrite so looking for help. I am trying to redirect URL substring from one host to another w/o recognition of the queries. For example,all calls with /irj/* should go to http://example.com/sso/?sourceURL=* . where "*" can be anything (has queries or has "?" sign (just sign, not a query parameter indicator)). I don't need to modify it anyhow or do any query transformation (as I will use the value of "sourceURL" after doing some logic inside "sso" application).

I managed to create the following .htaccess :
RewriteEngine on
RewriteRule ^irj/(.*)$ http://example.com/SSO/?sourceURL=$1 [NE,QSA]

It works fine although it tries to replace first "?" sign (regardless if it's a query indicator or just a character in the substring comes after /irj ) with "&". I tried w/o QSA but then I've got query parameters/values removed in the target substring.

Please help me to redirect the whole substring w/o making any changes to the target URL.

Thanks and regards,
Mike

[edited by: jdMorgan at 2:37 am (utc) on Nov. 22, 2008]
[edit reason] example.com [/edit]

jdMorgan

2:35 am on Nov 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can try this to see if it will "jam" an encoded "?" (%3f) into the original query string, and then append that to the one with the original URL in it. I don't know if it will work, because I've never tried doing this with an encoded character before. It does work with normal characters, though.

RewriteEngine on
RewriteCond \%3f%{QUERY_STRING} (.+)
RewriteRule ^irj/(.*)$ http://example.com/SSO/?sourceURL=$1%1 [R=302,L]

Your code at example.com/SSO/ is going to need to un-encode that second question mark; I encoded it because I'm fairly sure you cannot send two in the same query, as it is a 'reserved' character in HTTP. See RFC2396 [faqs.org], Section 2.2 - Reserved Characters.

Alternate method:


RewriteEngine on
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^irj/(.*)$ http://example.com/SSO/?sourceURL=$1\%3f%1 [NE,R=302,L]

If either or both of these methods work, the result will be that an encoded "?" will be used to begin the original query string when it is 'copied' into the new URL. This will happen whether or not there was a query string on the original URL. If that's not acceptable, let me know after you test the rules above -- These examples are just a 'proof of concept' at this point.

If you want to 'watch' the redirect happen and check the 'outgoing' URL, the Live HTTP Headers add-on for Firefox/Mozilla browsers is very handy for testing/debugging redirects.

Jim

mikeip

1:00 am on Nov 26, 2008 (gmt 0)

10+ Year Member



Hi Jim,

Thank you very much for a quick and correct reply. Both methods you suggested work fine. (in the first method I added "NE" directive to the [R=302,L]).

Regards,
Mike