0

I'm using mod_rewrite in a PHP web system and I have this rewrite rule in my .htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+).(pdf)$  /static/data/biblioteca/$1.$2 [L]

which allows me to write

<a href="http://my_domain.net/some_file.pdf" target="_new">title_of_some_file</a>

and have the PDF file opened directly in a new browser window, with the advantage of disguising my directory structure when the source page is seen.

This is working fine but for one thing. I live in Brazil and the Portuguee language has signs symbols "á", "é", "ã", "õ" and most of the the titles of the books available have these symbols.

When I use urlencode to obtain a proper filename it seems mod_rewirte gets confused, because then it redirects the file but I receive an error 404!

I know I may gve up the symbols, replacing "ã" by "a" and so on, but I do not want to do so, because some embarrasing mistakes may occur. For instance, in Portuguese we have "côco" meaning coconut and "cocô" meaning shit. If I replace "ô" by "o" both words will be written the same, with all possible (and disgusting) distortions of the meaning.

  • Apache decodes the querystring for you. See also: http://stackoverflow.com/q/459667/362536 – Brad Jun 10 '14 at 14:46
  • 1
    BTW: *"the advantage of disguising my directory structure..."* - the file can be reached via that public URL, who cares what the directory structure looks like? – deceze Jun 10 '14 at 14:46
  • Maybe I'm a fool, @deceze, by I just don't like people viewing all my directory structure. I always use mod_rewrite to avoid this. Besides, it shortens my URLs a bit. The goal of the page is to make certain PDF books available to the users of the site, for sure. As I said before, maybe I am just a bit fool. :) – ederpsampaio Jun 10 '14 at 14:51
  • What system do you use? I just tested and it's works fine on my Ubuntu/Linux OS – Peter Jun 10 '14 at 14:52
  • Thanks @Brad, I gonna see this post. – ederpsampaio Jun 10 '14 at 14:54
  • I'm using Windows 7... The fact is I'm using the development environment of my customer and he will not allow me to install Slackware, the development environment I like most. – ederpsampaio Jun 10 '14 at 14:55
  • @eder Well, as long as you don't expect any actual *security* benefits from it... :) – deceze Jun 10 '14 at 15:03

1 Answers1

2

As I said in comments, I just tested and it's works fine on my Ubuntu/Linux OS. So it looks like it's text encoding issue.

Try with [NE] (No Escape) flag

RewriteRule ^(.+).(pdf)$  /static/data/biblioteca/$1.$2 [L,NE]

If that not helps maybe you may consider to use some PHP

.htaccess:

RewriteRule ^(.+).(pdf)$  /readpdf.php?name=$1 # (example)

PHP part:

<?php

$filename = $_GET['name'];

// you may need to make sure there is no security flaws (like .. in name etc.)
// you may also need to convert encoding (from utf to iso for MS Windows)

$filepath = "static/data/biblioteca/" . $filename;
readfile($filepath . ".pdf");
Peter
  • 16,453
  • 8
  • 51
  • 77