[BNM] rewrite rules apache
Joe Aliferis
joealiferis at googlemail.com
Wed Nov 19 17:37:15 GMT 2008
wow !
thanks a bunch
I will try and implement this
Joe
> 2008/11/14 Joe Aliferis <joe at newforms.co.uk>:
>
>> I am trying to write a rewrite rule in an htaccess file
>>
>> I want to swap all instances of "&" in any URL to "&"
>>
>
> The solution below, using mod_rewrite, should work as long as you can
> install programs on your server to use with RewriteMap (you should be
> able to put the program anywhere, so FTP access to your home directory
> may be enough). It's not the most efficient solution possible (I guess
> that would be a specially-written Apache module) but not horribly
> inefficient either, because the RewriteMap program is only spawned
> once per server restart, not once per request.
>
> If you end up using this I'd be interested to hear how it works out.
> It's something I've thought of doing before but never got round to,
> perhaps because of my disdain for any browser (or spam spider?) that
> doesn't unescape links properly.
>
> First you need to put this script somewhere on your server, and make
> it executable:
>
> #!/bin/sh
> exec sed -ur 's/&(amp;)+/\&/g'
>
> This script assumes you have /bin/sh and sed on your server. If not,
> it's easy to replace in Perl or Python or whatever scripting language
> you have available. All it does is read newline-terminated query
> strings from stdin, and write newline-terminated corrected strings to
> stdout. It should use unbuffered I/O to avoid deadlocking, done here
> by the -u option to sed. You could extend it to unescape other entity
> references besides & if they're ever a problem.
>
> Then put the following in the relevant section of your Apache
> configuration (replacing the path to the script above):
>
> RewriteEngine on
> RewriteMap unescape-amp prg:/path/to/the/script/above
> RewriteCond %{QUERY_STRING} &
> RewriteRule ^(.*)$ $1?${unescape-amp:%{QUERY_STRING}} [R]
>
> This should fix up any "&"s in query strings for any URLs, which
> is where I assume they're occurring. If you have ampersands in the
> path parts of your URLs, you can catch them too by replacing the last
> two lines with:
>
> RewriteCond %{REQUEST_URI}?%{QUERY_STRING} &
> RewriteRule ^(.*)$ ${unescape-amp:$1}?${unescape-amp:%{QUERY_STRING}} [R]
>
> The script above replaces sequences like &amp;amp; with a single
> ampersand, to attempt to catch even more horrific failures of string
> processing. If you only want to replace single "&"s, just change
> the sed command to 's/&/\&/g'.
>
> The [R] option makes the browser visibly redirect to the corrected
> URL, which should reduce the propagation of mangled URLs.
>
> Hope this helps, and please let me know how it works out if you use it.
>
> Andy
>
More information about the BNMlist
mailing list. Powered by Wessex Networks