handling the "#" symbol in filenames - .htaccess

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

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.

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(.).

Related

htaccess redirect a new address with anchor

I need at the opening a address,
http://mysite.com/page.php
happened redirected to this address:
http://mysite.com/other_page.php#!blablabla
tell please, how to make this with .htaccess file ?
By default, special characters, such as & and ?, for example, will be converted to their hexcode equivalent. Using the [NE] flag prevents that from happening.
RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]
The above example will redirect /anchor/xyz to /bigpage.html#xyz. Omitting the [NE] will result in the # being converted to its hexcode equivalent, %23, which will then result in a 404 Not Found error condition.
see also this question:
Htaccess redirect

Htaccess redirect is not working in my site

Iam trying to redirect the url
mysite/love/this-is-another-question-about-lurv/29481751ffe886a64f9b0c9a
to
mysite/question.php?qkey=29481751ffe886a64f9b0c9a.
Iam using the following rewrite rule, but it is not working
RewriteEngine On
RewriteRule ([\w]+)/([\w]+)/^([\w]+) question.php?cat=$1&qtitle=$2&qkey=$3
If I recall, a dash character isn't considered a word character, try:
RewriteRule ([\w-]+)/([\w-]+)/^([\w-]+) question.php?cat=$1&qtitle=$2&qkey=$3
also I'm not sure what function that caret is playing in the last part. Outside of a [] it's normally used to indicate the start of a line, if that's the case here, than it would necessarily fail.
in which case:
RewriteRule ([\w-]+)/([\w-]+)/([\w-]+) question.php?cat=$1&qtitle=$2&qkey=$3
may perhaps be a working solution. I'd double check that - is a word character.

RewriteRule and the number sign “#”

I'm trying to make a friendly URL through RewriteRule but it keeps ignoring # as part of the variable value.
The line on .htaccess is as simple as this
RewriteRule ^key/(.+)/$ index.php?key=$1
and the requested URL is
http://www.example.com/key/c%23/
but I'm only getting c as get variable and not c%23.
What exactly am I doing wrong?
Finally after some digging, I managed to pull this off.
It just needs the B flag on RewriteRule to escape non-alphanumeric characters such as #
RewriteRule ^key/(.+)/$ index.php?key=$1 [B]
%23 is a hash symbol (#), so it (and anything after it) doesn't actually get parsed by mod_rewrite. The actual URL therefore is http://www.foo.com/key/c, without any %23. Other dash-codes work fine, though.
%23 is a hash mark (#). I'm guessing the browser is interpreting the hash as an anchor and not passing it on to the server. For instance, if you user http://www.foo.com/key/c%20/ you'll get "c[space]".

How to target file with %20 in .htaccess?

Here is my .htaccess:
...
rewriterule file%20name.htm http://mysite.com/new_name.htm#anchor [r=301,nc,ne]
...
It's not redirected, and I believe this is because it's not targeting the file with the %20 in the name. As for the flags, I used ne to not rewrite the #anchor portion of the redirect.
Also, the flag "nu" breaks my file and I get internal server 500 error.
Thanks you.
When mod_rewrite is invoked, the path has already been decoded as part of Apache's request processing. So, if you want to check for a space, you can do so literally:
RewriteRule file\ name\.html http://example.com/new_name.htm#anchor [R=301,NC,NE,L]
The NU flag doesn't exist, which is why you get an internal server error when you try to use it.
Not the best answer, and can only be used in specific cases, but I changed
file%20name.htm
to
file.*
so it will match any filename that starts with "file", regardless of spaces and special chars.

How do I use space and non-ascii characters in .htaccess files

in .htaccess imposibla use space, non-Latin characters and etc. ?
For example how, fix this:
RewriteRule ^/(.*)/new user/(.*)$ /page/?id=$1 [QSA,L,E]
this decision is not me:
... /new_user/ ...
Thanks
You have to escape the reserved characters for URIs with their respective character codes in percent-encoding, e. g. %20 for a space.
See http://en.wikipedia.org/wiki/Percent-encoding for more details.
OK .. signs allowed in url:
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
and in htaccess:
RewriteRule ^(([0-9]+)-)?new_user(-([0-9]+))?/?$ new_user.php?page=$2&id=$4 [L]
change www.blabla.com/10-new_user-5
to www.blabla.com/new_user.php?page=10&id=5
The URL is automatically decoded before your rules operate on it, so you can test against literal characters, e.g. français\ español (note that you need to escape literal spaces with a backslash, unless the pattern is quoted).
However, based on your example, I think your actual concern is being able to do something like this:
RewriteRule ^([^/]+)/[^/]+/.*$ /page/?id=$1 [QSA,L]
# ^---^
# it doesn't matter what the user name is, as long as it
# doesn't contain a slash

Resources