What's wrong with a colon: in a ###comment? - .htaccess

When I added
deny from 82.118. ### 82.118.242.240 (country: BG)
to .htaccess, subsequent attempts to visit my site resulted in a 500 server error.
Simply changing the colon : to a dash -
deny from 82.118. ### 82.118.242.240 (country- BG)
now works fine.
But I'm not googling any problem with colons in comments.
So I tried just changing a comment line from
### added automatically...
to
### added: automatically...
just adding an extraneous colon after "added". And that causes no problems at all. So what's going on, and what's the general rule that my "deny from" with colon is violating?

Apache's configuration format doesn't support inline comments. Comments should always go on their own line in .htaccess. Putting a comment after conditions/rules will cause them to be treated like a parameter.

Related

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

htaccess dash in rewrite causing issues

I cant seem to get my htaccess code right for redirecting pages that appear like the below to go to a 410 page, eg:
www.domain.com/-c-23.html
www.domain.com/-c-12.html
www.domain.com/-c-755.html
Basically, I want a rule whereby anything where "-c-" comes directly after the slash of the domain gets sent to a 410. I have been trying to do this but my code isn't working, so far I have something like this:
RewriteRule ^/\-c-[0-9]+\.html$ - [G]
But this makes no difference at all, any idea why this is not working?
You need to get rid of the leading slash in your regular expression. URI's sent through rules in htacccess files have the leading slash stripped off:
RewriteRule ^\-c-[0-9]+\.html$ - [G,L]
You also need the L flag to immediately stop rewriting. You could also be even more general:
RewriteRule ^-c- - [G,L]

Using .htaccess to style URL directory style

I have searched this question and looked around but can't seem to get this working in practice. This is my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /poker/(.*)/(.*)/$ /poker/?$1=$2
I am trying to get my page to work like this:
mysite.com/poker/page/home
But this just isn't working, I have used 3 different generators and tried typing it manually from tutorials but it is just returning a 404. Any idea's a greatly appreciated, it could be really obvious..
Thanks
You do not have a trailing slash in your example, yet your rule requires one. You can make the trailing slash optional:
RewriteEngine on
RewriteRule /poker/(.*)/(.*)/?$ /poker/?$1=$2
Note however, that a uri /poker/a/b/c/d/e/f/g/ is also a match here - a/b/c/d/e/f will match the first subpattern and g will match the second one, because (.*) is greedy. Be more specific if you wish to match only content between slashes - e.g. ([^/]*)
Well, there's really nothing wrong with the rules that you have if http://mysite.com/poker/?page=home resolves correctly. The only thing is that if this is in an htaccess file, the leading slash is removed from the URI when it's matched against in a RewriteRule, so you need to remove it from your regular expression (or maky it optional):
RewriteRule ^poker/(.+)/(.+)/?$ /poker/?$1=$2
And maybe make the groupings (.+) instead so that there is at least one character there.

Removing unwanted characters from URL in htaccess

Our current htaccess setup correctly converts urls like this: site.com/page.php?sid=Friend to site.com/Friend
However, due to an unrelated oversight, we had almost all of our URLs double-indexed as site.com/Friend> Because the greater than sign is a special character it doesn't call page.php so the > needs to be stripped out in htaccess and can't be done on page.php. Compounding matters is that the way they're indexed is as: site.com/Friend%3E which also might need to be stripped out.
What we would like is to have another directive that looks for an ending of > (or %3E), strips it off, then redirects to the variable that's there without that ending > In essence so that site.com/Friend> (or site.com/Friend%3E) still points to site.com/Friend
Thank you for your help.
Add this to the top of your rules:
RewriteRule ^/?(.*)>$ /$1 [L,R=301]
You can use > because the URI gets decoded when matching in a RewriteRule.

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.

Resources