RewriteRule and the number sign “#” - .htaccess

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]".

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

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.

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.

How to redirect a URL containing smartquotes via .htaccess?

Is there a way to redirect a URL containing smart quotes via .htaccess? I'm using the following rules. Only the last one seems to work:
RewriteRule ^8-%E2%80%9Crules%E2%80%9D-for-social-advertising$ /8-rules-for-social-advertising [R=301,L]
RewriteRule ^8-“rules”-for-social-advertising$ /8-rules-for-social-advertising [R=301,L]
RewriteRule ^8-%25E2%2580%259Crules%25E2%2580%259D-for-social-advertising$ /8-rules-for-social-advertising [R=301,L]
When I surf to http://blog.eloqua.com/8-“rules”-for-social-advertising/ or http://blog.eloqua.com/8-%E2%80%9Crules%E2%80%9D-for-social-advertising it doesn't get redirected.
But if I go to http://blog.eloqua.com/8-%25E2%2580%259Crules%25E2%2580%259D-for-social-advertising everything works just fine.
What am I doing wrong? Thanks so much for your help!
You are right, it's slipping passed the rules you have provided.
The reason is because the unicode characters represented by the %E2%80%9C and %E2%80%9D (aka microsoft smartquotes) have already been turned into their unicode representation within Apache. As such you need to properly match the bytestream representing those characters within apache.
In order to properly redirect urls such as this:
http://www.example.com/8-%E2%80%9Crules%E2%80%9D-for-social-advertising
You would use a rule like this:
http://www.example.com/8-\xE2\x80\x9Crules\xE2\x80\x9D-for-social-advertising

301 htaccess redirect dynamic url help needed

I'm trying to redirect this
hhttp://www.website.net/forum/index.php?showtopic=12345
to
hhttp://www.website.ORG/forum/t12345
12345 being the dynamic topic ID
I also need any information to be stripped away if it is found after the topic ID, for example
hhttp://www.website.net/forum/index.php?showtopic=12345&view=getlastpost
I want &view=getlastpost or any similar that may appear after the ID number to be get rid of.
I've tried
RewriteCond %{QUERY_STRING} ^(([^&]&))showtopic=([^&]+)&?(.*)?$
RewriteRule ^index.php$ http://www.website.org/forum/t%3?%1%4/ [L,R=301]
but it didn't work. I get trash in the URL.
hhttp://www.website.org/forum/index.php?showtopic=29294&view=getlastpost (when that link is clicked - the result is hhttp://www.website.net/forum/t29294?view=getlastpost/)
hhttp://www.website.org/forum/index.php?showtopic=29029 (when that link is clicked - the result is hhttp://www.website.net/forum/t29029?/).
How can I clear it out?
$2 implies there are two bracketed areas, but I only see one in your rule, so changed that to $1.
Also your URL starts /forum/ so need to include that in the rule.
And the . in index.php needs to be escaped if you don't want it treated as a regex special character.
And if you want to ditch anything after the showtopic=1234 then just remove the $ that indicates the end of the string
RewriteRule ^forum/index\.php?showtopic=([0-9]*) http://www.website.org/forum/t$1/ [L,R=301]

Resources