URI rewrite ( replace backslash with forward slash )

sasanka

New Member
Messages
3
Reaction score
0
Points
0
I have a flash menu in my website which works good when using IE but not when using FireFox. Reason is Flash (SWF) file has resource path specified in the form \\xxx\\menudata.xml. IE replace the forward slashes before sending the request to the server but not FF. Unfortunately I do not have the source for the SWF so I cannot change it.

Other option is to change it on the fly, therefore I am trying to use "rewrite" in .htaccess to replace "\\" with "/" as follows,

RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)\\(.*)$
RewriteRule .* %1/%2 [R=301,NC,L]

I have tested the above code in Ubuntu11.04/Apache2.2 and it works fine there but not here in X10Hosting.


CPANEL ERROR MESSAGE as below
File not found [/home/sasankav/public_html/includes\js\menudata.xml

I am unable to figure out anything and have no clue about what could be wrong here, Please help and thanks in advance.

Sasanka.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Backslashes should be URI-escaped in URI paths, so you'll need to account for this in your rewrite condition:

Code:
RewriteCond %{REQUEST_URI} ^(.*)(\\|%5C)(.*)$
RewriteRule .* %1/%3 [R=301,NC,L]

However, using Apache to change backlashes to forward slashes is inefficient as it will only change one character per request, resulting in one extra requests per backslash in the URL path for each resource. Even though you don't have access to the source for the flash movie, you can try a hex editor, flash decompiler or SWF editor to change the resource. Failing that, pipe any URI paths through a script to generate the 301 redirect:

Code:
RewriteCond %{REQUEST_URI} (\\|%5C)
RewriteRule (.*) fixslashes.php?uri=$1 [L]

fixslashes.php:
PHP:
<?php
/* The "uri" query param is unnecessary, but using it is less magical & mysterious */
if (isset($_REQUEST['uri'])) {
    $path = $_REQUEST['uri'];
} else {
    $path = $_SERVER['REQUEST_URI'];
}
$path = preg_replace('/\\+|%5C/', '/', $path);
header('Location: ' . $path, True, 302);

A rewrite map would be useful here, but you can't define external rewriting programs in .htaccess.
 
Last edited:
Top