Redirect apache urls with special characters - apache

I need this redirection with mod_rewrite, but I don't have any success.
The url is http://www.domain.com/descargar catálogo
I thought this would work:
RewriteRule ^descargar%20catálogo$ /url/whatever.php [NC,NE]
Any solution for this. I have tried with NE and without, etc...
This gives me internal error:
RewriteRule ^descargar catálogo$ /url/whatever.php [NC,NE]
Other things I tried but didn't work:
RewriteRule ^descargar%20cat%C3%A1logo /url/whatever.php
RewriteRule ^descargar\ cat%C3%A1logo /url/whatever.php

URLs cannot contain non-ASCII characters. Escaping the character to represent the actual way the URL is transmitted should do it:
cat%C3%A1logo

Related

How to allow special characters in Rewrite_Map?

Here is the config I am using
RewriteEngine on
RewriteMap shortlinks txt:/var/www/html/s.overhash.net/public_html/shortlinks.txt
RewriteRule ^/(.+)$ ${shortlinks:$1} [R=temp,L]
My txt document looks something like this:
9H40o https://osyrisrblx.github.io/playground/#code/HYUw7gBAggTjCGBPAPMArgWwEYhgPgAoBKAOhhABM0BjEAggB3IDcAaCatOEYAFyIC8eJiGYBqAZ258iQA
However, upon going to my site (http://s.overhash.net/9H40o), it replaces the # with %23, making the URL this:
https://osyrisrblx.github.io/playground/%23code/HYUw7gBAggTjCGBPAPMArgWwEYhgPgAoBKAOhhABM0BjEAggB3IDcAaCatOEYAFyIC8eJiGYBqAZ258iQA
(which isn't valid)
How would I go about ensuring the # remains?
It's not a rewrite map issue, it's the way rewrite rules work.
By default, special chars will be escaped.
Use the NE flag in your rewriterule if you don't want that to happen:
RewriteRule ^/(.+)$ ${shortlinks:$1} [NE,R=temp,L]
More details on https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_ne

.htaccess -> Characters not escaping in rewrite string "(" ")"

I'm currently trying to add some redirects from a very old site that had PDFs and some of these PDFs have used brackets to contain the years.
For example:
/document%2070C11-name%20(2012).pdf
So the full URL I am trying to redirect is:
https://website.co.uk/document/1/document%2070C11-name%20(2012).pdf
I have previously setup many different redirects on this site but ones with the special characters seem to be causing an issue.
Here is the examples of what I have tried, and failed with so far:
RewriteEngine on
RewriteBase /
RewriteRule ^document/1/document%2070C11-name%20\(2008\).pdf$ document/newurl/ [R=301,L]
RewriteRule ^document/1/document%2070C11-name%20(.*)2008(.*).pdf$ document/newurl/ [R=301,L]
So I tried escaping the character and just trying a wildcard. Neither seemed to work on my Apache server. The code I used did seem to work when I tested it on:
http://htaccess.madewithlove.be/
But I am pretty stuck now and would love any help I can get.
Thanks,
Kane
To match %MN character in URL, you need to use \xMN in the RewriteRule pattern.
Hence this rule should work for you:
RewriteRule ^document/1/document\x2070C11-name\x20\(2008\)\.pdf$ /document/newurl/ [R=301,NC,NE,L]
This will redirect https://website.co.uk/document/1/document%2070C11-name%20(2008).pdf to https://website.co.uk/document/newurl/

special characters match in htaccess redirect .com?

I trying to create a SEO friendly url in the format like this
http://web_url/news/**news_title**/**newsid**
which is redirected to
http://web_url/index.php?t=news&newsid=newsid
I wrote the following .htaccess rule but it fails as long as the url format has no special characters but once a special character is present in the url, it fails
eg. http://web_url/news/mywebsite.com is live!/4
Here are my rules:
RewriteRule ^news/([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?t=news&newsid=$2
RewriteRule ^news/([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?t=news&newsid=$2
Any help would be greatly appreciated
Just use
RewriteRule ^news/([^/]+)/([0-9]+)/?$ index.php?t=news&newsid=$2
instead of both rules.

How do you match percent symbols in rewrite rules?

I am redirecting URLs for a website, and have the following URL to match and redirect:
example.com/test-string-%2526-more-string/
However, I cannot match the %2526 portion of the string - it seems that Apache is treating the percentage symbol as a special character, and escaping it seems to make no difference. Here's the basic rule I'm trying:
RewriteRule ^test-string-\%2526-more-string/?$ /new-location.htm [R=301,L,NC]
With or without the escaping slash, this rule doesn't fire. I suspect the problem is with how apache processes encoded characters in URLs - %25 translates to a percentage symbol itself, so could Apache be translating "%25" to "%" before running the rule? Or is something else going on?
I experimented with lots of rewrite rules and in the end just went with this:
RewriteRule ^test-string-\%26-more-string/?$ /new-location.htm [R=301,L,NC]
So the %25 was converted to a % symbol.

Apache: Why on IE .htaccess rewrites non Latin character URLs to urlencode? How to solve it?

Apache: Why on IE .htaccess rewrites non Latin character URLs to urlencode? How to solve it?
domain.com//контакты rewrites to domain.com/%D0%BA%D0%BE%D0%BD%D1%82%D0%B0%D0%BA%D1%82%D1%8B
RewriteEngine On
# Rewrite multiple slashes with single slash after domain
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,NE,L]
Seems there still are no solution for IE and it depends of browser address-bar encoding.
Like Chrome 12 shows UTF-8 url domain.com/контакты, but on Ctr+C, Ctr+P you will get Unicode url http://domain.com/%D0%BA%D0%BE%D0%BD%D1%82%D0%B0%D0%BA%D1%82%D1%8B
#Pekka: Yes. On newest browsers any URLs has UTF-8 encoding. And they rewrites domain.com//контакты to domain.com/контакты, like it is excepted to be.
As far as I know, this is not entirely correct and this may be the reason why it doesn't work for you.
What new browsers are really doing is they show you the URL in its native form контакты, but internally still make the request in percent encoded form.
This seems to be the crux: You type something in, IE recognizes the Unicode characters, but the subsequent 301 redirect leads to a (correct) percent encoded URL. A modern browser will automatically "decode" the percent encoded URL; IE and older browsers might not.
I don't know whether there is a workaround for this. One thing you could try is have a PHP script do the URL rewriting, and have that script redirect to the (however invalid!) URLDecoded URL:
<?php header("location: http://example.com/контакты"); ?>
I have no idea whether this'll work, though.
Reference: Unicode characters in URLs
Related IE setting