rayan
rayan

Reputation: 21

handling the "#" symbol in filenames

Songs that contain a "#" in the track title give me a 404 error while trying to download from my site. How to fix this?

This is my current .htaccess code:

RewriteRule ^download/([^/]*)-([^/]*).mp3$ download.php?download=$2&pl=$1

Upvotes: 2

Views: 73

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

From the Apache manual

By default the special characters such as ?, #will be converted to their hexcode.Using the [NE] flag prevents that from happening.

 RewriteRule ^download/([^/]*)-([^/]*)\.mp3$ download.php?download=$2&pl=$1 [NE,QSA,NC,L]

The above example will redirect /download-foo-bar.mp3 to download.php?download=bar&pl=bar#1 . Omitting the [NE] flag will result in the # being converted to its hexcode %23., which will then result in 404 not found error condition.

And in RewriteRule pattern ,dot (.) is a special characters, it matches any characters. Escape it using a backslash if you want to match a literal dot(.).

Upvotes: 0

Daniel Unterberger
Daniel Unterberger

Reputation: 171

The # is never reaching apache. It is a jump-marker and everything in the url after # is not sent to the webserver. The javascript:document.location.hash is jumping to an anchor in the page. You can escape the # with %23 which then should reach your webserver even without htaccess. As you use php, you can use htmlentities($filename) or urlencode($filename) to fix it during output.

Upvotes: 3

Related Questions