Forum Moderators: phranque

Message Too Old, No Replies

.htaccess, mod_rewrite and %2F (/)

         

Jesse_Smith

3:23 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it possible to use mod_rewrite when the / is changed to %2F in the URL?

The URL is

domain.com/section/MusicTracks/search/word/word/word

but the browser shows...

domain.com/section/MusicTracks/search/word%2Fword%2Fword

RewriteRule ^section/MusicTracks/search/(.*)$ cgi-local/amazon_products_feed.cgi?Operation=ItemSearch&SearchIndex=MusicTracks&Keywords=$1 [L]
RewriteRule ^section/MusicTracks/search/(.*)/(.*)$ cgi-bin/file.cgi?Operation=ItemSearch&SearchIndex=MusicTracks&Keywords=$1 [L]
RewriteRule ^section/MusicTracks/search/(.*)/(.*)/(.*)$ cgi-bin/file.cgi?Operation=ItemSearch&SearchIndex=MusicTracks&Keywords=$1 [L]

Even

RewriteRule ^section/MusicTracks/search/(.*)\%2F(.*)$ cgi-bin/file.cgi?Operation=ItemSearch&SearchIndex=MusicTracks&Keywords=$1 [L]
RewriteRule ^section/MusicTracks/search/(.*)\%2F(.*)\%2F(.*)$ cgi-bin/file.cgi?Operation=ItemSearch&SearchIndex=MusicTracks&Keywords=$1 [L]

doesn't work.

jdMorgan

4:15 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, this is possible -- as a matter of fact, I detect encoded characters in several dozen rules on several of my sites.

A couple of points, though:
1) In all cases, I'm seeing encoded slashes as lowercase, i.e. %2f.
2) Your use of ".*" -- the most 'greedy' and most ambiguous pattern, may be contributing to your trouble.

Fixing both problems makes your longest rule look like this:


RewriteRule ^section/MusicTracks/search/([^/%]+)(/¦\%2f)([^/%]+)(/¦\%2f)(.*)$ cgi-bin/file.cgi?Operation=ItemSearch&SearchIndex=MusicTracks&Keywords=$1 [NC,L]

This should work for plain slashes or encoded slashes, upper- or lowercase.

Note that I added two parenthesized subexpressions in order to match "/" or "\%2f", so your back-reference numbering will have to take that into account if you intend to capture two or more parameters. I also used forward-looking negative matches to speed up the processing, i.e. "[^/%]+" means, "match one or more characters not equal to a slash or a percent sign." This is much, much faster that using ".*" because the parser will only have to process the pattern in a forward direction. Using ".*" would force it to process your string many, many times, and is very inefficient.

The percent sign should not have to be escaped within [] but it does have to be escaped within ().

Replace the broken pipe "¦" characters with solid pipes before use!

Jim

Jesse_Smith

4:55 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Replace the broken pipe "�" characters with solid pipes before use!

What are broken pipe/solid pipe characters?!

jdMorgan

5:09 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hoo boy! We've got character-encoding differences, too...

On a U.S. 101-key keyboard, the pipe character is located above the right shift key, and would be accessed with Shift plus \ key.

It is ASCII code %7c (hexadecimal), also called a vertical bar.

mod_rewrite requires the character-code of %7c, but posting on WebmasterWorld changes it to some other code -- and trying to use that other code will cause mod_rewrite to fail with a 500-Server Error.

Jim

Jesse_Smith

7:43 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ugggg! I can't even copy that on to a text file! '�' messes it up! So I save the file and turn it into a text file and the character changes to ¶.

¦¦¦¦ does work.