php variables not interpreted during rewrites - php

I've been tasked with helping provide the infrastructure for an external facing website and things are mostly fine, it's deployed and puppet control and hooray for my standards etc.
However, it gets tricky because we are provided the code for the website by an external developer and they quickly noticed that 'rewrites are not working'
I spent some time checking into this and rewrites for apache using an .htaccess file are indeed working perfectly.
Where it falls down is that php variables are not being passed or interpreted during a rewrite operation
I posted a previous stackoverflow question with extremely detailed info and have since then simplified down my example considerably.
My mod_rewrite rules do not resolve PHP variables
nobody was able to determine the issue in my previous thread, so I've simplified my case somewhat for testing.
my basic troubleshooting .htaccess file is here
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^test/test2/$ test.php?var=test2&var2=test [L]
RewriteRule ^hello.html redir.html [L]
</ifModule>
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
and when accessing
http://server.example.net/hello.html
I'm correctly taken to my redir.html page. So I know rewrites work and I checked that by tailing the log file to confirm as well.
Now the problem, when I run my the following
http://server.example.net/test/test2/
it should redirect to my test.php file
http-//server.example.net/test.php?var=test2&var2=test which is a really basic file
<?php
echo $_GET['var'].'aa'.$_GET['var2'];
?>
now if I browse to the location without using rewrites I get the html page as follows
test2aatest
which shows the 2 variables being parsed and displayed on the page. But if I visit with the url that uses rewrites
http-//server.example.net/test/test2/
then I get the following
Notice: Undefined index: var in /var/www/itnews/test.php on line 4
Notice: Undefined index: var2 in /var/www/itnews/test.php on line 4
aa
which tells me that I am correctly redirected to the page, but there is something wrong with the variables.
If it helps
OS - Centos 7 x64 and all latest updates
php packages
php.x86_64 5.4.16-36.el7_1 #base
php-cli.x86_64 5.4.16-36.el7_1 #base
php-common.x86_64 5.4.16-36.el7_1 #base
php-gd.x86_64 5.4.16-36.el7_1 #base
php-mbstring.x86_64 5.4.16-36.el7_1 #base
php-mysql.x86_64 5.4.16-36.el7_1 #base
php-pdo.x86_64 5.4.16-36.el7_1 #base
php-snmp.x86_64 5.4.16-36.el7_1 #base
apache
httpd.x86_64 2.4.6-40.el7.centos #base
httpd-tools.x86_64 2.4.6-40.el7.centos #base
lastly, my rewrite log.
[Mon Feb 22 13:10:16.901115 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test.php -> /var/www/itnews/test.php/test2/
[Mon Feb 22 13:10:16.901131 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test.php/test2/ -> test.php/test2/
[Mon Feb 22 13:10:16.901135 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] applying pattern '^test/test2/$' to uri 'test.php/test2/'
[Mon Feb 22 13:10:16.901140 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test.php -> /var/www/itnews/test.php/test2/
[Mon Feb 22 13:10:16.901144 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test.php/test2/ -> test.php/test2/
[Mon Feb 22 13:10:16.901147 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] applying pattern '^hello.html' to uri 'test.php/test2/'
[Mon Feb 22 13:10:16.901156 2016] [rewrite:trace1] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] pass through /var/www/itnews/test.php
[Mon Feb 22 13:10:16.901241 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test.php -> /var/www/itnews/test.php/test2/
[Mon Feb 22 13:10:16.901251 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test.php/test2/ -> test.php/test2/
[Mon Feb 22 13:10:16.901255 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] applying pattern '^test/test2/$' to uri 'test.php/test2/'
[Mon Feb 22 13:10:16.901274 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test.php -> /var/www/itnews/test.php/test2/
[Mon Feb 22 13:10:16.901279 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test.php/test2/ -> test.php/test2/
[Mon Feb 22 13:10:16.901282 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] applying pattern '^hello.html' to uri 'test.php/test2/'
[Mon Feb 22 13:10:16.901286 2016] [rewrite:trace1] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] pass through /var/www/itnews/test.php
[Mon Feb 22 13:10:16.901375 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test2 -> /var/www/itnews/test2/
[Mon Feb 22 13:10:16.901387 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test2/ -> test2/
[Mon Feb 22 13:10:16.901391 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] applying pattern '^test/test2/$' to uri 'test2/'
[Mon Feb 22 13:10:16.901395 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test2 -> /var/www/itnews/test2/
[Mon Feb 22 13:10:16.901401 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test2/ -> test2/
[Mon Feb 22 13:10:16.901405 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] applying pattern '^hello.html' to uri 'test2/'
[Mon Feb 22 13:10:16.901408 2016] [rewrite:trace1] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] pass through /var/www/itnews/test2
note, I'm in ops and I'm not familiar with php so I'm working on second hand info about how php works exactly.

Write below line
RewriteRule ^(.*)/(.*)$ /test.php?var=$1&var2=$2 [NC,L]
instead of
RewriteRule ^test/test2/$ test.php?var=test2&var2=test [L] // incorrect
Hope it will help you :)

Related

RewriteRule Does Not Yield Expected Substitution

Everything is going perfect. It looks like it understands what I want, but then something I can't understand begins to happen. My .htaccess looks like this:
RewriteEngine On
RewriteBase "/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^contacts/edit(new|existing)$ /index.php?route=contacts/edit$1 [NC,L,QSA]
The first relevant line in the log is ->
[Wed Apr 07 14:23:41.495232 2021] [rewrite:trace2] [pid 2302] mod_rewrite.c(483): [client ::1:13883] ::1 - - [localhost/sid#209cc4c8][rid#23090058/initial] [perdir /path/to/document/root/] rewrite 'contacts/editNew' -> '/index.php?route=contacts/editNew', referer: http://localhost/
Comment: It looks at the request URI and attempts to substitute it to 'index.php?route=...'
Line #2 ->
[Wed Apr 07 14:23:41.495256 2021] [rewrite:trace3] [pid 2302] mod_rewrite.c(483): [client ::1:13883] ::1 - - [localhost/sid#209cc4c8][rid#23090058/initial] split uri=/index.php?route=contacts/editNew -> uri=/index.php, args=route=contacts/editNew, referer: http://localhost/
Comment: It tries to split the uri into URI and parameters.
Line #3 ->
[Wed Apr 07 14:23:41.495283 2021] [rewrite:trace2] [pid 2302] mod_rewrite.c(483): [client ::1:13883] ::1 - - [localhost/sid#209cc4c8][rid#23090058/initial] [perdir /document/root/path/] trying to replace prefix /path/to/document/root/ with /, referer: http://localhost/
Comment: mod_rewrite just translates the DOCUMENT_ROOT path to "/".
Line #4 ->
[Wed Apr 07 14:23:41.495311 2021] [rewrite:trace2] [pid 2302] mod_rewrite.c(483): [client ::1:13883] ::1 - - [localhost/sid#209cc4c8][rid#23090058/initial] [perdir /path/to/document/root/] trying to replace context docroot /path/to/document/root with context prefix , referer: http://localhost/
Comment: I do not know what this is. I would like someone to, please, explain what is going on in this line. Strangely, there is a space between prefix and the comma. Does this mean anything?
Line #5 ->
[Wed Apr 07 14:23:41.495334 2021] [rewrite:trace1] [pid 2302] mod_rewrite.c(483): [client ::1:13883] ::1 - - [localhost/sid#209cc4c8][rid#23090058/initial] [perdir /path/to/document/root/] internal redirect with /index.php [INTERNAL REDIRECT], referer: http://localhost/
Comment: This is the redirect to /index.php, which I believe is correct.
Line #6 ->
[Wed Apr 07 14:23:41.495834 2021] [rewrite:trace3] [pid 2302] mod_rewrite.c(483): [client ::1:13883] ::1 - - [localhost/sid#209cc4c8][rid#23091b18/initial/redir#1] [perdir /path/to/document/root/] strip per-dir prefix: /path/to/document/root/index.php -> index.php, referer: http://localhost/
Comment: I believe this is taking an absolute URL path and making it relative. Is this standard? Do you have to specify this in your code or does it happen by default?
Line #7 ->
[Wed Apr 07 14:23:41.495900 2021] [rewrite:trace3] [pid 2302] mod_rewrite.c(483): [client ::1:13883] ::1 r- - [localhost/sid#209cc4c8][rid#23091b18/initial/redir#1] [perdir /path/to/document/root/] applying pattern '^contacts/edit(new|existing)$' to uri 'index.php', referer: http://localhost/
Comment: This is what has got me puzzled. '^contacts/edit(edit|existing)$' is applied to uri 'index.php', but not to 'index.php?route=contacts/edit$1', which is what, I believe, I have indicated in my second rewrite rule. So, why doesn't this occur??
Finally, and I have no idea why this happens, the entire URL gets translated to "http://contacts/editnew"! Even localhost gets removed.
I would really appreciate if you could help me out with this.

Remove server.php using htaccess got error 500

I have a website that can be accessed in two ways:
http://localhost/public/blog
http://localhost/server.php/blog
I use the code below to remove the "public" from the url, and its work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
So I tried to change public/ to server.php/ from above code. But it's does not work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ server.php/$1 [L]
</IfModule>
Server return error code 500.
The server log log reports:
[Wed Sep 04 01:06:53.801523 2019] [core:error] [pid 546] [client
182.253.16.222:36164] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use
'LimitInternalRecursion' to increase the limit if necessary. Use
'LogLevel debug' to get a backtrace.
So my expectation from the second code is address "http://localhost/server.php/blog" can be access without server.php in there (http://localhost/blog).
Both examples you showed have valid syntax and should work, but you seem to still have problems, which suggests that the two different redirect patterns are interfering with each other somehow, or that you have a circular redirection.
Validating if your configuration works correctly
I've just set up a minimalistic apache project for experimenting with rewrite rules: https://github.com/rpagliuca/apache-rewrite-minimalist-environment
In that project, I tested out a configuration very similar to what you described:
# Apache virtual host config file: vhost.conf
<VirtualHost *:80>
DocumentRoot /var/www/html
RewriteEngine On
LogLevel alert rewrite:trace6
RewriteRule ^(.*)$ /server.php$1 [L]
</VirtualHost>
Also, I've added 2 different PHP files to the project:
# File 1: server.php
Handled by server.php<br/>
URI: <?= $_SERVER['REQUEST_URI'] ?>
The second file is similar:
# File 1: server.php
Handled by index.php<br/>
URI: <?= $_SERVER['REQUEST_URI'] ?>
So I started the project with docker-compose up --build, found out the IP of the container using docker ps and docker inspect, and tested out the following URL: http://192.168.16.2/blog. I got the following output on the browser:
Handled by server.php
URI: /blog
This test shows that this RewriteRule is valid in simple cases (where there is no circular redirection -- for scenarios with circular redirection, please read #Anonymous answer).
If I comment out the line RewriteEngine On line, I get a 404 error on the URL http://192.168.16.2/blog, which supports the conclusion that the redirect rule is indeed working. Also, index.php is only accessible when the RewriteEngine On is commented out. Otherwise, when the redirect engine is enabled, I get the following output:
Handled by server.php
URI: /index.php
The ultimate solution
You might have noticed the line LogLevel alert rewrite:trace6 in the vhost.conf file above.
That line enables debugging for your rewrite rules for Apache 2.4 or later. With that, you can fix your future redirect rules errors with way more ease and speed.
Whenever I run my server with docker-compose up --build, and hit the /blog URI, I get the following output:
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121396 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] init rewrite engine with requested uri /blog
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121497 2019] [rewrite:trace3] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] applying pattern '^(.*)$' to uri '/blog'
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121534 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] rewrite '/blog' -> '/server.php/blog'
app_1_d7cddb74e8bd | 192.168.16.2:80 192.168.16.1 - - [04/Sep/2019:12:27:02 +0000] "GET /blog HTTP/1.1" 200 267 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36"
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121550 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] local path result: /server.php/blog
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121618 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] prefixed with document_root to /var/www/html/server.php/blog
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.121630 2019] [rewrite:trace1] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7737780a0/initial] go-ahead with /var/www/html/server.php/blog [OK]
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122014 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] init rewrite engine with requested uri /blog
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122032 2019] [rewrite:trace3] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] applying pattern '^(.*)$' to uri '/blog'
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122048 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] rewrite '/blog' -> '/server.php/blog'
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122060 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] local path result: /server.php/blog
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122079 2019] [rewrite:trace2] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] prefixed with document_root to /var/www/html/server.php/blog
app_1_d7cddb74e8bd | [Wed Sep 04 12:27:02.122091 2019] [rewrite:trace1] [pid 17] mod_rewrite.c(483): [client 192.168.16.1:49620] 192.168.16.1 - - [192.168.16.2/sid#7fa770113d98][rid#7fa7736c30a0/subreq] go-ahead with /var/www/html/server.php/blog [OK]
You can follow theses logs, and easily find out where is happening your circular redirection or any other misintended behaviours.
After fixing it, remember to turn off mod_rewrite debugging, especially on production environments.
You’re redirecting every URL, but you should only redirect when server.php is missing.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^server\.php($|/)
RewriteRule ^(.*)$ server.php/$1 [L]
</IfModule>

How do I use mod_rewrite to both redirect and rewrite a directory?

Don't flag this as a duplicate unless you know what you are doing. Some questions look similar, but aren't what I'm looking for.
If a user hits example.com/friendly-folder it should be internally redirected to example.com/system-folder.
However if a user hits the old example.com/system-folder it should be externally redirected to the new name example.com/friendly-folder.
So, basically, the directory URL must always be friendly, but pointing to the old one. I believe if I just made two rules without any condition it would get stuck in an infinite loop.
I've tried this in my .htaccess
RewriteRule ^friendly-folder(.*)$ system-folder$1 [NC,L]
RewriteCond %{THE_REQUEST} ^GET\ /system-folder
RewriteRule ^system-folder(.*)$ /friendly-folder$1 [R=301,NC,L]
Which effectively internally redirects from the friendly folder to the system one, but doesn't externally redirect from the system folder to the friendly one.
Any ideas? While you are at it, if you have a method that also works for redirecting and rewriting files it would be much appreciated.
Update
Now i've tried this configuration:
RewriteRule ^friendly-folder(.*)$ system-folder$1 [NC,END]
RewriteRule ^system-folder(.*)$ friendly-folder$1 [R=301,NC,L]
And I'm getting this error log:
[Sun Jan 15 18:40:49.765190 2017] [rewrite:trace3] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] strip per-dir prefix: C:/xampp/htdocs/urls/system-folder/ -> system-folder/
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace3] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] applying pattern '^friendly-folder(.*)$' to uri 'system-folder/'
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace3] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] strip per-dir prefix: C:/xampp/htdocs/urls/system-folder/ -> system-folder/
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace3] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] applying pattern '^system-folder(.*)$' to uri 'system-folder/'
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace2] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] rewrite 'system-folder/' -> 'friendly-folder/'
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace3] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] add per-dir prefix: friendly-folder/ -> C:/xampp/htdocs/urls/friendly-folder/
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace2] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] explicitly forcing redirect with http://localhost/C:/xampp/htdocs/urls/friendly-folder/
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace1] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] escaping http://localhost/C:/xampp/htdocs/urls/friendly-folder/ for redirect
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace1] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] redirect to http://localhost/C:/xampp/htdocs/urls/friendly-folder/ [REDIRECT/301]
[Sun Jan 15 18:40:49.771194 2017] [core:error] [pid 3936:tid 1896] (20023)The given path was above the root path: [client ::1:50822] AH00127: Cannot map GET /C:/xampp/htdocs/urls/friendly-folder/ HTTP/1.1 to file

How to pass a Persian string as a argument in the URL?

I have a URL like this pattern:
www.example.com/ClassName/MethodName/Arg1/Arg2
Also here is my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
ErrorDocument 404 /error404.html
And this is my routing system:
if (empty($_GET['rt'])) {
require_once('application/home.php');
} else {
require_once('application/search.php');
$url = rtrim ($_GET['rt'], '/');
$url = explode('/', $url);
$ClassName = array_shift($url);
$MethodName = array_shift($url);
$Arg1 = array_shift($url);
$Arg2 = array_shift($url);
}
Now what is the problem? Well, Everything is fine ..! Routing is completely fine for every URLs except when I use م in the URL. (م is a Persian character)
For e.g.
www.example.com/ClassName/Methodname/124/روز خوب // it is fine
www.example.com/ClassName/Methodname/254/سلام بر // it isn't fine
// because there is م ^ in the URL
So when I use م in the URL, I will faced with 404 Not Found page:
Well, I don't know that problem comes from where .. do you know? And how can I fix it? Is it a encoding issue? Or what?
Note: I use Xampp v3.2.1 (apache).
EDIT: As mentioned in the comment, I add these two examples:
<?php
$str = "www.example.com/ClassName/Methodname/124/روز خوب";
$url = explode('/', $str);
echo "<pre>";
print_r($url);
/*
Array
(
[0] => www.example.com
[1] => ClassName
[2] => Methodname
[3] => 124
[4] => روز خوب
)
*/
Two: (this directs to 404 Not Found)
<?php
$str = "www.example.com/ClassName/Methodname/254/سلام بر";
$url = explode('/', $str);
echo "<pre>";
print_r($url);
/*
Array
(
[0] => www.example.com
[1] => ClassName
[2] => Methodname
[3] => 254
[4] => سلام بر
)
*/
EDIT2: According to a few tests, I figured out the script that should get called by my rewriting rules (index.php), it even doesn't get call.
EDIT3: I enabled rewrite logging on Apache and when I check the result, there is a interesting thing:
(These two samples aren't related to the above examples)
Working routing sample:
[Sat Jan 02 22:17:00.276918 2016] [rewrite:trace3] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#28661a0/initial] [perdir C:/xampp/htdocs/myweb/] add path info postfix: C:/xampp/htdocs/myweb/islamic_sources -> C:/xampp/htdocs/myweb/islamic_sources/sahifeh_sajadiyeh/1580/\xd9\x86\xd8\xa8\xd9\x88\xd8\xaa, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.276918 2016] [rewrite:trace3] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#28661a0/initial] [perdir C:/xampp/htdocs/myweb/] strip per-dir prefix: C:/xampp/htdocs/myweb/islamic_sources/sahifeh_sajadiyeh/1580/\xd9\x86\xd8\xa8\xd9\x88\xd8\xaa -> islamic_sources/sahifeh_sajadiyeh/1580/\xd9\x86\xd8\xa8\xd9\x88\xd8\xaa, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.276918 2016] [rewrite:trace3] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#28661a0/initial] [perdir C:/xampp/htdocs/myweb/] applying pattern '^(.*)$' to uri 'islamic_sources/sahifeh_sajadiyeh/1580/\xd9\x86\xd8\xa8\xd9\x88\xd8\xaa', referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.276918 2016] [rewrite:trace4] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#28661a0/initial] [perdir C:/xampp/htdocs/myweb/] RewriteCond: input='C:/xampp/htdocs/myweb/islamic_sources' pattern='!-f' => matched, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.276918 2016] [rewrite:trace4] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#28661a0/initial] [perdir C:/xampp/htdocs/myweb/] RewriteCond: input='C:/xampp/htdocs/myweb/islamic_sources' pattern='!-d' => matched, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.276918 2016] [rewrite:trace2] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#28661a0/initial] [perdir C:/xampp/htdocs/myweb/] rewrite 'islamic_sources/sahifeh_sajadiyeh/1580/\xd9\x86\xd8\xa8\xd9\x88\xd8\xaa' -> 'index.php?rt=islamic_sources/sahifeh_sajadiyeh/1580/\xd9\x86\xd8\xa8\xd9\x88\xd8\xaa', referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.276918 2016] [rewrite:trace3] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#28661a0/initial] split uri=index.php?rt=islamic_sources/sahifeh_sajadiyeh/1580/\xd9\x86\xd8\xa8\xd9\x88\xd8\xaa -> uri=index.php, args=rt=islamic_sources/sahifeh_sajadiyeh/1580/\xd9\x86\xd8\xa8\xd9\x88\xd8\xaa, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.277919 2016] [rewrite:trace3] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#28661a0/initial] [perdir C:/xampp/htdocs/myweb/] add per-dir prefix: index.php -> C:/xampp/htdocs/myweb/index.php, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.277919 2016] [rewrite:trace2] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#28661a0/initial] [perdir C:/xampp/htdocs/myweb/] strip document_root prefix: C:/xampp/htdocs/myweb/index.php -> /myweb/index.php, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.277919 2016] [rewrite:trace1] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#28661a0/initial] [perdir C:/xampp/htdocs/myweb/] internal redirect with /myweb/index.php [INTERNAL REDIRECT], referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.277919 2016] [rewrite:trace3] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#286c8b0/initial/redir#1] [perdir C:/xampp/htdocs/myweb/] strip per-dir prefix: C:/xampp/htdocs/myweb/index.php -> index.php, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.277919 2016] [rewrite:trace3] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#286c8b0/initial/redir#1] [perdir C:/xampp/htdocs/myweb/] applying pattern '^(.*)$' to uri 'index.php', referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.277919 2016] [rewrite:trace4] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#286c8b0/initial/redir#1] [perdir C:/xampp/htdocs/myweb/] RewriteCond: input='C:/xampp/htdocs/myweb/index.php' pattern='!-f' => not-matched, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.277919 2016] [rewrite:trace1] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#286c8b0/initial/redir#1] [perdir C:/xampp/htdocs/myweb/] pass through C:/xampp/htdocs/myweb/index.php, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.470250 2016] [rewrite:trace3] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#286c1b8/initial] [perdir C:/xampp/htdocs/myweb/] strip per-dir prefix: C:/xampp/htdocs/myweb/fonts/taha/QuranTaha.woff -> fonts/taha/QuranTaha.woff, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.470250 2016] [rewrite:trace3] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#286c1b8/initial] [perdir C:/xampp/htdocs/myweb/] applying pattern '^(.*)$' to uri 'fonts/taha/QuranTaha.woff', referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.470250 2016] [rewrite:trace4] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#286c1b8/initial] [perdir C:/xampp/htdocs/myweb/] RewriteCond: input='C:/xampp/htdocs/myweb/fonts/taha/QuranTaha.woff' pattern='!-f' => not-matched, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
[Sat Jan 02 22:17:00.470250 2016] [rewrite:trace1] [pid 3188:tid 1728] mod_rewrite.c(475): [client ::1:49413] ::1 - - [localhost/sid#c397b0][rid#286c1b8/initial] [perdir C:/xampp/htdocs/myweb/] pass through C:/xampp/htdocs/myweb/fonts/taha/QuranTaha.woff, referer: http://localhost/myweb/search?s=islamic_sources&q=%D8%B3%D9%84%D8%A7%D9%85
Not working (redirects to 404 not found) sample:
[Sat Jan 02 22:07:09.734092 2016] [rewrite:trace3] [pid 3188:tid 1712] mod_rewrite.c(475): [client ::1:64955] ::1 - - [localhost/sid#c397b0][rid#83ec138/initial] [perdir C:/xampp/htdocs/myweb/] add path info postfix: C:/xampp/htdocs/myweb/islamic_sources -> C:/xampp/htdocs/myweb/islamic_sources/sahifeh_sajadiyeh/306/\xd9\x85\xd8\xa8
[Sat Jan 02 22:07:09.734092 2016] [rewrite:trace3] [pid 3188:tid 1712] mod_rewrite.c(475): [client ::1:64955] ::1 - - [localhost/sid#c397b0][rid#83ec138/initial] [perdir C:/xampp/htdocs/myweb/] strip per-dir prefix: C:/xampp/htdocs/myweb/islamic_sources/sahifeh_sajadiyeh/306/\xd9\x85\xd8\xa8 -> islamic_sources/sahifeh_sajadiyeh/306/\xd9\x85\xd8\xa8
[Sat Jan 02 22:07:09.734092 2016] [rewrite:trace3] [pid 3188:tid 1712] mod_rewrite.c(475): [client ::1:64955] ::1 - - [localhost/sid#c397b0][rid#83ec138/initial] [perdir C:/xampp/htdocs/myweb/] applying pattern '^(.*)$' to uri 'islamic_sources/sahifeh_sajadiyeh/306/\xd9\x85\xd8\xa8'
[Sat Jan 02 22:07:09.734092 2016] [rewrite:trace1] [pid 3188:tid 1712] mod_rewrite.c(475): [client ::1:64955] ::1 - - [localhost/sid#c397b0][rid#83ec138/initial] [perdir C:/xampp/htdocs/myweb/] pass through C:/xampp/htdocs/myweb/islamic_sources
Interesting point: when routing is fine, that Persian string will be like this: (just decode):
%D8%B3%D9%84%D8%A7%D9%85
But when routing is 404 not found, the Persian string will be like this:
\xd9\x86\xd8\xa8\xd9\x88\xd8\xaa
Seems there is two different kinds of encoding ..
You could try this variant that may work better:
RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]
The changes that this makes are:
1: using [\s\S] to match absolutely any character, instead of . which matches anything but a newline.
Though you wouldn't normally expect newline (%0A) to be in your URLs, my suspicion is that Apache's regexp matcher is treating your input path as being in the ISO-8859-1 encoding.
The IRI character U+0645 Arabic Letter Meem م UTF-8-URL-encodes to URI sequence %D9%85, and whilst byte 0xD9 is okay in ISO-8859-1, 0x85 decodes to U+0085 Next Line (NEL), an undesirable legacy control character that often counts as a newline. So if that happened, the expression .* wouldn't match it.
Having said all that, this is quite theoretical as your example works as-is for me, on an old XAMPP 1.8.2 I had lying about on WinXP.
2: using the [B] rewrite flag, to ensure all bytes are passed in correctly-URL-encoded form in the parameter.
Otherwise, non-ASCII characters would break for situations where Apache sends the query string to PHP through Windows environment variables. The Windows environment is Unicode, so Apache has to decode the bytes on writing and PHP has to encode them again on reading, and unfortunately those encodings don't match.
Apache uses ISO-8859-1 and PHP (via C stdlib) uses the ANSI code page, which depends on the locale of the Windows installation. On a Western install, you get code page 1252, which is close to ISO-8859-1 so only some of the bytes will be wrong (again, this includes the 0x85 in م); on other locales with other ANSI code pages all the non-ASCII characters will be wildly wrong.
This doesn't necessarily apply to you as XAMPP is using mod_php, which doesn't need to use the environment to pass strings. But it would make a difference in other hosting environments. In any case, without [B] you'll find URL-special characters in the string (ampersand, plus, percent) break the query parser.
Use URL encoder:
String encodedURL = "www.example.com/ClassName/Methodname/254/" + URLEncoder.encode("Any text in any language", "utf-8");
UTF-8 of course is the encoding to use.
You can read more here: https://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html

Specific kanji gets wrongly encoded during http get request

I am having a really weird encoding issue when using kanji for the application I am developing; all kanji characters except the character "免" work fine.
When I try to use this character, mongodb throws the following exception:
Exception: non-utf8 string: �
Any other kanji character works fine.
I have tried to remove the functions processing the string, and input it directly to the function handling mongodb but to no success.
I have also tried to convert the character from various encodings, but to no avail.
This is what I am using:
Phalcon 2.0 - https://phalconphp.com
Apache 2.4.12
PHP 5.6.8
MongoDB 3.0.4
Thanks!
Controller function handling the request
public function kanjiAction($findKanji = "")
{
// Really weird issue with the kanji 免 not working
$findKanji = $this->filter->sanitize($findKanji, 'string');
$findKanji = mb_substr($findKanji, 0, 1, 'UTF-8');
$kanjiHash = md5($findKanji);
// If entry does not exist in cache, create it
if(!$this->viewCache->exists($kanjiHash)) {
// Get data from DB
$kanji = Kanji::findById($findKanji);
$words = Words::containingKanji($findKanji);
$showWords = array();
foreach ($words as $word) {
foreach ($word->k_ele["keb"] as $key => $value) {
if(strpos($value, $findKanji) != false) {
$showWords[] = (object) array(
"word" => $value,
"readings" => $word->r_ele["reb"],
"meanings" => $word->sense["gloss"]
);
break;
}
}
}
// Fetch translations
$this->view->onTitle = $this->translation->query('onTitle');
$this->view->kunTitle = $this->translation->query('kunTitle');
$this->view->radicalTitle = $this->translation->query('radicalTitle');
$this->view->meaningsTitle = $this->translation->query('meaningsTitle');
$this->view->strokeCountTitle = $this->translation->query('strokeCountTitle');
$this->view->jlptTitle = $this->translation->query('jlptTitle');
$this->view->containsWordsTitle = $this->translation->query('containsWordsTitle');
$this->view->readingsTitle = $this->translation->query('readingsTitle');
$this->view->wordsTitle = $this->translation->query('wordsTitle');
$this->view->notFound = $this->translation->query('notFound');
// Pass data to view
$this->view->kanji = $kanji->_id;
if(isset($kanji->reading["on"]) && count($kanji->reading["on"]) > 0)
$this->view->onReadings = $kanji->reading["on"];
if(isset($kanji->reading["kun"]) && count($kanji->reading["kun"]) > 0)
$this->view->kunReadings = $kanji->reading["kun"];
if(isset($kanji->radicals))
$this->view->radicals = $kanji->radicals;
if(isset($kanji->attributes['jlpt']))
$this->view->jlpt = $kanji->attributes['jlpt'];
if(isset($kanji->attributes['stroke_count']))
$this->view->strokeCount = $kanji->attributes['stroke_count'];
if(isset($kanji->meaning))
$this->view->meanings = $kanji->meaning;
if(count($showWords) > 0)
$this->view->words = $showWords;
$this->view->kanjiHash = $kanjiHash;
} else {
$this->view->kanjiHash = $kanjiHash;
}
}
Request Header for non-working kanji:
GET /view/kanji/%E5%85%8D HTTP/1.1
Host: localhost
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4
Response header for non-working kanji:
HTTP/1.1 200 OK
Date: Fri, 03 Jul 2015 13:04:05 GMT
Server: Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8
X-Powered-By: PHP/5.6.8
Content-Length: 29
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Request Header for working kanji:
GET /view/kanji/%E5%A8%A9 HTTP/1.1
Host: localhost
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4
Response header for working kanji:
HTTP/1.1 200 OK
Date: Fri, 03 Jul 2015 13:16:20 GMT
Server: Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8
X-Powered-By: PHP/5.6.8
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
Edit:
Stack trace for 免:
#0 [internal function]: MongoCursor->rewind()
#1 [internal function]: Phalcon\Mvc\Collection::_getResultset(Array, Object(Purple\Model\Kanji), Object(MongoDB), true)
#2 [internal function]: Phalcon\Mvc\Collection::findFirst(Array)
#3 E:\xampp\htdocs\app\Purple\Controller\ViewController.php(33): Phalcon\Mvc\Collection::findById('\xE5')
#4 [internal function]: Purple\Controller\ViewController->kanjiAction('\xE5')
#5 [internal function]: Phalcon\Dispatcher->dispatch()
#6 E:\xampp\htdocs\public\index.php(98): Phalcon\Mvc\Application->handle()
#7 {main}
Edit 2:
[Mon Jul 06 18:05:59.294298 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] add path info postfix: E:/xampp/htdocs/view -> E:/xampp/htdocs/view/kanji/\xe5\x85\x8d
[Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] strip per-dir prefix: E:/xampp/htdocs/view/kanji/\xe5\x85\x8d -> view/kanji/\xe5\x85\x8d
[Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] applying pattern '^$' to uri 'view/kanji/\xe5\x85\x8d'
[Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] add path info postfix: E:/xampp/htdocs/view -> E:/xampp/htdocs/view/kanji/\xe5\x85\x8d
[Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] strip per-dir prefix: E:/xampp/htdocs/view/kanji/\xe5\x85\x8d -> view/kanji/\xe5\x85\x8d
[Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] applying pattern '(.*)' to uri 'view/kanji/\xe5\x85\x8d'
[Mon Jul 06 18:05:59.294798 2015] [rewrite:trace2] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] rewrite 'view/kanji/\xe5\x85\x8d' -> 'public/view/kanji/\xe5'
[Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] add per-dir prefix: public/view/kanji/\xe5 -> E:/xampp/htdocs/public/view/kanji/\xe5
[Mon Jul 06 18:05:59.294798 2015] [rewrite:trace2] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] strip document_root prefix: E:/xampp/htdocs/public/view/kanji/\xe5 -> /public/view/kanji/\xe5
[Mon Jul 06 18:05:59.294798 2015] [rewrite:trace1] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] internal redirect with /public/view/kanji/\xe5 [INTERNAL REDIRECT]
[Mon Jul 06 18:05:59.296799 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] add path info postfix: E:/xampp/htdocs/public/view -> E:/xampp/htdocs/public/view/kanji/\xe5
[Mon Jul 06 18:05:59.296799 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] strip per-dir prefix: E:/xampp/htdocs/public/view/kanji/\xe5 -> view/kanji/\xe5
[Mon Jul 06 18:05:59.317801 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] applying pattern '^(.*)$' to uri 'view/kanji/\xe5'
[Mon Jul 06 18:05:59.318301 2015] [rewrite:trace2] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] rewrite 'view/kanji/\xe5' -> 'index.php?_url=/view/kanji/\xe5'
[Mon Jul 06 18:05:59.318301 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] split uri=index.php?_url=/view/kanji/\xe5 -> uri=index.php, args=_url=/view/kanji/\xe5
[Mon Jul 06 18:05:59.318301 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] add per-dir prefix: index.php -> E:/xampp/htdocs/public/index.php
[Mon Jul 06 18:05:59.318301 2015] [rewrite:trace2] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] strip document_root prefix: E:/xampp/htdocs/public/index.php -> /public/index.php
[Mon Jul 06 18:05:59.318301 2015] [rewrite:trace1] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] internal redirect with /public/index.php [INTERNAL REDIRECT]
[Mon Jul 06 18:05:59.318301 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f0d7d8/initial/redir#2] [perdir E:/xampp/htdocs/public/] strip per-dir prefix: E:/xampp/htdocs/public/index.php -> index.php
[Mon Jul 06 18:05:59.318301 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f0d7d8/initial/redir#2] [perdir E:/xampp/htdocs/public/] applying pattern '^(.*)$' to uri 'index.php'
[Mon Jul 06 18:05:59.318801 2015] [rewrite:trace1] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f0d7d8/initial/redir#2] [perdir E:/xampp/htdocs/public/] pass through E:/xampp/htdocs/public/index.php
Edit 3:
[Tue Jul 07 01:25:18.844429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] add path info postfix: E:/xampp/htdocs/view -> E:/xampp/htdocs/view/kanji/\xe5\x85\x8d
[Tue Jul 07 01:25:18.844429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] strip per-dir prefix: E:/xampp/htdocs/view/kanji/\xe5\x85\x8d -> view/kanji/\xe5\x85\x8d
[Tue Jul 07 01:25:18.844429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] applying pattern '^$' to uri 'view/kanji/\xe5\x85\x8d'
[Tue Jul 07 01:25:18.844429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] add path info postfix: E:/xampp/htdocs/view -> E:/xampp/htdocs/view/kanji/\xe5\x85\x8d
[Tue Jul 07 01:25:18.844429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] strip per-dir prefix: E:/xampp/htdocs/view/kanji/\xe5\x85\x8d -> view/kanji/\xe5\x85\x8d
[Tue Jul 07 01:25:18.844929 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] applying pattern '(.*)' to uri 'view/kanji/\xe5\x85\x8d'
[Tue Jul 07 01:25:18.844929 2015] [rewrite:trace2] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] rewrite 'view/kanji/\xe5\x85\x8d' -> 'public/view%2fkanji%2f%e5'
[Tue Jul 07 01:25:18.844929 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] add per-dir prefix: public/view%2fkanji%2f%e5 -> E:/xampp/htdocs/public/view%2fkanji%2f%e5
[Tue Jul 07 01:25:18.844929 2015] [rewrite:trace2] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] strip document_root prefix: E:/xampp/htdocs/public/view%2fkanji%2f%e5 -> /public/view%2fkanji%2f%e5
[Tue Jul 07 01:25:18.844929 2015] [rewrite:trace1] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] internal redirect with /public/view%2fkanji%2f%e5 [INTERNAL REDIRECT]
[Tue Jul 07 01:25:18.849429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] add path info postfix: E:/xampp/htdocs/public/view -> E:/xampp/htdocs/public/view/kanji/\xe5
[Tue Jul 07 01:25:18.849429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] strip per-dir prefix: E:/xampp/htdocs/public/view/kanji/\xe5 -> view/kanji/\xe5
[Tue Jul 07 01:25:18.849429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] applying pattern '^(.*)$' to uri 'view/kanji/\xe5'
[Tue Jul 07 01:25:18.849429 2015] [rewrite:trace2] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] rewrite 'view/kanji/\xe5' -> 'index.php?_url=/view%2fkanji%2f%e5'
[Tue Jul 07 01:25:18.849429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] split uri=index.php?_url=/view%2fkanji%2f%e5 -> uri=index.php, args=_url=/view%2fkanji%2f%e5
[Tue Jul 07 01:25:18.849429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] add per-dir prefix: index.php -> E:/xampp/htdocs/public/index.php
[Tue Jul 07 01:25:18.849429 2015] [rewrite:trace2] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] strip document_root prefix: E:/xampp/htdocs/public/index.php -> /public/index.php
[Tue Jul 07 01:25:18.849429 2015] [rewrite:trace1] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] internal redirect with /public/index.php [INTERNAL REDIRECT]
[Tue Jul 07 01:25:18.849930 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f2d448/initial/redir#2] [perdir E:/xampp/htdocs/public/] strip per-dir prefix: E:/xampp/htdocs/public/index.php -> index.php
[Tue Jul 07 01:25:18.849930 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f2d448/initial/redir#2] [perdir E:/xampp/htdocs/public/] applying pattern '^(.*)$' to uri 'index.php'
[Tue Jul 07 01:25:18.850430 2015] [rewrite:trace1] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f2d448/initial/redir#2] [perdir E:/xampp/htdocs/public/] pass through E:/xampp/htdocs/public/index.php
From your stack trace, it appears that something is stripping off part of the $findKanji before it reaches your controller. Instead of the three bytes that represent 免, there is only the first of those in the call to kanjiAction.
The other kanji that failed also contained a 0x85 byte in it, so I wonder if something before you hit your controller is stripping all bytes once it hits byte 0x85.
So now you need to see what might be interfering with your parameter before it gets to your controller. Do you have any event listeners attached to dispatch:beforeDispatchLoop or to any of the dispatch events? (If so, what?)
I will note that I just made a phalcon app from scratch (followed the tutorial) to see the stack trace I'd get and I got the stack trace:
Exception: Because I can
File=/var/www/tutorial/app/controllers/ViewController.php
Line=15
#0 [internal function]: ViewController->kanjiAction('???')
#1 [internal function]: Phalcon\Dispatcher->dispatch()
#2 /var/www/tutorial/public/index.php(39): Phalcon\Mvc\Application->handle()
#3 {main}
Note the three characters being passed to kanjiAction: if I hit the url /view/kanji/%E5 I only get one character.
That was with a simplified controller that existed just to give a stack trace:
<?php
use Phalcon\Mvc\Controller;
class ViewController extends Controller
{
public function indexAction()
{
}
public function kanjiAction($findKanji = "")
{
var_dump($findKanji);
throw new Exception('Because I can');
}
}
This is using Phalcon 2.0.1. So we know that a vanilla phalcon app passes all three bytes on down to the handler, at least on my setup of ubuntu and Apache 2.2.
Looking at the difference between my setup (that apparently works) and yours, I'm wondering if the difference is something to do with the Apache rewrite rules on Windows. So let's turn the debugging on the rewrite process way up. Since you have Apache 2.4, that's done by putting this directive:
LogLevel alert rewrite:trace3
Into your <VirtualHost> section in your server config file.
Once you do that, update the answer with the lines in the apache log that contain [rewrite: that are generated when you try to access one of these erroring kanji.
EDIT: And now that you've included that information, I think I have a fix.
I don't know why you're seeing the problem that you are. I'm going to separately try to reproduce it so that I can file a bug with apache (though I may not be able to until I get a windows box), but in the meantime I think I have a fix.
First, in the <VirtualHost> section in your server config file add this directive:
AllowEncodedSlashes On
Then, assuming that you have your .htaccess files set up as in the Phalcon tutorial, add the B option to the second RewriteRule in the top directory's .htaccess file, so that the line looks like:
RewriteRule (.*) public/$1 [B,L]
Then also add the B flag to the RewriteRule line in the .htaccess file in the public/ directory, so that the line becomes:
RewriteRule ^(.*)$ index.php?_url=/$1 [B,QSA,L]
That second change might not be necessary; since I don't have a system with your exact problem, I can't check.
Still no luck, but let's try one more thing: turn (.*) into ((?s).*) so that the lines modified before now read:
RewriteRule ((?s).*) public/$1 [B,L]
and
RewriteRule ^((?s).*)$ index.php?_url=/$1 [B,QSA,L]
Assuming that this works, the reasoning behind it is bizarre.
Apparently, character 0x85 is a "next line" character in Unicode, and can be treated by PCRE (the regex engine underlying Apache's rewrite functionality) as a newline character, as detailed here. The normal way that PCRE is compiled is to treat only character 10 as a newline, (\n) but PCRE can be compiled to treat more characters as a newline character.
When a character is a "newline" character, the regex metacharacter . won't match it, unless the s regex option is active. The (?s) bit makes that option active.
If this works, you can eliminate the B option on the RewriteRules, and remove the AllowEncodedSlashes directive from your <VirtualHost>.
Are you using a locally compiled apache binary, or did you get one from an official place? Because if you got one from an official place, you should complain that them compiling PCRE with this strange option wasted your weekend.

Categories