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

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/

Related

htaccess rewrite rule with strings that contain slashes

I have a mod rewrite rule like the following.
RewriteRule ^bands/([^/]*)$ /folder/index.php?palabra=$1 [L]
It works except when someone access a link that uses a word with a / symbol on the band name, for example the band ac/dc would not work and shows a 404 error.
Im guessing some kind of problem with the regex.
How can I avoid this problem?

How to include a single $ (dollar-sign) within the rewrite rule

I'm trying to create a rewrite rule for my .htaccess file. I want to include a single dollar sign within it. It is supposed to be something like this:
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^\$$ "http\:\/\/domain\.com\/something" [R=301,L]
The problem is that it doesn't work with a single dollar sign, so if I go to domain.com/$ I get a 404. It works only with another letter, for example ^\$a$ - in such case domain.com/$a would redirect to domain.com/something.
The workaround is to create a new folder, rename it to $ and put the .htaccess file there, but I would rather avoid creating multiple folders with no content for such purposes. I couldn't find any reference on the Internet (the official Apache documentation for mod_rewrite was not very helpful). I tried using multiple slashes in different combinations but everything failed. Am I missing something or is it just impossible to make it work this way?
For me it works exactly as expected in htaccess with RewriteRule ^\$$ and requesting "$". Have you looked at the rewritelog / loglevel rewrite:trace8 yet?

Apache and Japanase website re-write module

I use to work on a multilingual website, within the next few months, we added Korean and we are now adding Japanese. Apart the fact that it generate a huge list of 404 errors on the logs files, I am having another issue with the urls. The Apache server response with a Bad request when I insert this url:
学校-詳細な評価-11
#[.htaccess]
RewriteRule ^学校-詳細な評価-(.+)$ /statistics.php?n=$1 [L]
I tried also using the [B] flag, but nothing changes.
I tried as well to remove character and I found that it only works if I do in this way:
#[.htaccess]
RewriteRule ^学-(.+)$ /statistics.php?n=$1 [L]
But of course I cannot avoid the issue in this way :D
I notice that Apache will convert the url to:
%E5%AD%A6%E6%A0%A1-%E8%A9%B3%E7%B4%B0%E3%81%AA%E8%A9%95%E4%BE%A1-11
Then I tried it as well:
#[.htaccess]
RewriteRule ^%E5%AD%A6%E6%A0%A1-%E8%A9%B3%E7%B4%B0%E3%81%AA%E8%A9%95%E4%BE%A1-(.+)$ /statistics.php?n=$1 [L]
But I'm still having the issue :(
Does anyone know how to fix it or why this is happening? I did a research but all forums and website only suggest to use the flag B at the end of the url.
Any help is welcome! Thank you :)
Try in Hex:
RewriteRule ^\xE5\xAD\xA6\xE6\xA0\xA1-\xE8\xA9\xB3\xE7\xB4\xB0\xE3\x81\xAA\xE8\xA9\x95\xE4\‌​xBE\xA1-(.+)$ /statistics.php?n=$1 [L]
For example with having this rule:
RewriteRule ^\xE5\xAD\xA6-(.+)$ /statistics.php [L]
this url http://domain.com/学-t will redirect to http://domain.com/statistics.php
For testing you can use online htaccess tester: http://htaccess.madewithlove.be/ and remember to change the page encoding to UTF-8 before testing.

htaccess rewrite hide .php and also flatten query string on one particular url

I have checked various topics and nothing caught my eyes. This is what am trying to do ..
It's a small site and with only few pages all in my root /mobile folder. So I decided to modify h*p://example.com/mobile/academics.php to h*p://example.com/mobile/academics (without the trailing slash)
RewriteRule ^([^.]+)$ $1.php [NC,L] - Works fine.
But I have http://example.com/mobile/program.php?p=ams which I want to convert as http://example.com/mobile/program/ams . I tried this :
RewriteRule ^/program/([^/]+)$ program.php?p=$1 - Makes no effect. Browser keeps looking for /program/ams.php
How to have both rules coexist? I have query string only on program.php . Any help is appreciated. I am sorry if this has been answered before. I searched for quite sometime and couldn't find any.
Thanks,
Vik
use
RewriteRule ^/?program/([^/]+)$ program.php?p=$1

simple(?) htaccess rewriting issues

OK I have been messing about with URL rewriting for the last few days and seem to have come to a bit of a dead-end. I have come up with a few solutions that work on some servers and not others, and my hosting company (1and1 - be vary wary of these guys if you choose them as hosts) hasn't been able to help at all.
My problem is this, i want to re-write this URL:
/result.php?section=[section name]&url=[url]
to this (adding a trailing slash if there is none):
/article/[section name]/[url]/
and ALSO
section.php?section=[section name]
to (again adding a trailing slash):
/section/[section name]/
each attempt seems to have different results. currently I am using thhe following, which works locally, but on the live server only the 'articles' rewrite works:
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /article/$1/$2/
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /result.php?section=$1&url=$2
RewriteRule ^section/([a-zA-Z0-9_-]+)$ /section/$1/
RewriteRule ^section/([a-zA-Z0-9_-]+)/$ section.php?section=$1
Can anyone help me come up with a solution that will work nicely? Thank you in advance, I am really struggling with what seems like something relatively straightforward...
Firstly in order to force the backslash on the end you will need to actually do a redirect...
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /article/$1/$2/ [R=302,L]
RewriteRule ^section/([a-zA-Z0-9_-]+)$ /section/$1/ [R=302,L]
Note: I use 302 because 301 can be a pain during testing, once everything is working and you are happy with it change it to 301.
The two remaining rules are the rewrites that simply mask/alias the query_string URL, which should look something like this...
RewriteRule ^article/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /result.php?section=$1&url=$2
RewriteRule ^section/([a-zA-Z0-9_-]+)/$ /section.php?section=$1