I have a XAMPP Apache server, and have added a configuration file to rewrite URLs with a trailing slash and redirect to their slash-less counterparts.
Therefore, a url like http://example.com/the-audio/ gets redirected to http://example.com/the-audio. The problem is, it's not working when the directory name is only one word.
So, http://example.com/audio/ does not get it's slash removed. This is really strange for me, and checking the logs it appears as though the rule isn't matched in this case.
Here's my rule (only one in the file)
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)\/+$
RewriteRule ^ %1 [R=301,L]
And this is the error dump
error.log
[Wed Jan 11 22:13:32.729812 2017] [rewrite:trace3] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#29a2190/initial] [perdir C:/xampp/htdocs/tecnoedu/] strip per-dir prefix: C:/xampp/htdocs/tecnoedu/audio/ -> audio/
[Wed Jan 11 22:13:32.730312 2017] [rewrite:trace3] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#29a2190/initial] [perdir C:/xampp/htdocs/tecnoedu/] applying pattern '^' to uri 'audio/'
[Wed Jan 11 22:13:32.730312 2017] [rewrite:trace1] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#29a2190/initial] [perdir C:/xampp/htdocs/tecnoedu/] pass through C:/xampp/htdocs/tecnoedu/audio/
[Wed Jan 11 22:13:32.730312 2017] [rewrite:trace1] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#33046a0/subreq] [perdir C:/xampp/htdocs/tecnoedu/] pass through C:/xampp/htdocs/tecnoedu/audio/index.php
Thanks in advance!
According to your log /audio/ is an existing directory. So your rule wouldn't run since the first condition stops it running on existing directories. Remove the first condition to resolve it.
Also, to do this on existing directories, you will need to turn DirectorySlash off, or the server will add the slash back in itself. Note that there is some security concern with that.
Related
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.
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>
Apache: 2.4.25 (local)
PHP-FPM: 5.6.30
This scenario fails under Apache 2.4.25 but works under Apache 2.4.20.
Test URL:
https://server/some-app/fun
And directory contents of:
drwxr-xr-x 118 xx root 4012 May 18 16:12 ..
-rw-r--r-- 1 xx root 155 May 18 16:15 index.php
-rwxr-xr-x 1 xx root 450 May 18 16:25 .htaccess
drwxr-xr-x 4 xx root 136 May 18 16:25 .
I expect index.php to be served up, and "/fun" to be passed as a GET parameter.
.htaccess OK
This version of .htaccess serves up index.php with no problems, and the GET parameters show "/fun" as expected:
RewriteEngine on
RewriteCond $1 !^(index.php)
RewriteRule ^(.*)$ index.php?/$1 [L,NS]
index.php is served, and $_GET is:
Array
(
[/fun] =>
)
.htaccess 404
RewriteEngine on
RewriteCond $1 !^(index.php)
RewriteRule ^(.*)$ index.php/$1 [L,NS]
This one results in a 404, with PHP-FPM giving an error of:
[proxy_fcgi:error] [client 192.168.200.1:59812] AH01071: Got error 'Primary script unknown\n'
Notice the difference is that I removed the question mark from the RewriteRule's substitution.
Relevant FCGI config:
<IfModule !mod_php5.c>
<FilesMatch \.php$>
SetHandler "proxy:unix:///var/run/php-fpm-xyz.sock|fcgi://xyz/"
</FilesMatch>
</IfModule>
Timeout 3000
DirectoryIndex index.php
Sanitized Logs
[ssl:info] AH01964: Connection to child 18 established (server servername:443)
[authz_core:debug] mod_authz_core.c(809): AH01626: authorization result of Require all granted: granted
[authz_core:debug] mod_authz_core.c(809): AH01626: authorization result of <RequireAny>: granted
[perdir /data/www/some-app/] strip per-dir prefix: /data/www/some-app/fun -> fun
[perdir /data/www/some-app/] applying pattern '^(.*)$' to uri 'fun'
[perdir /data/www/some-app/] RewriteCond: input='fun' pattern='!^(index.php)' => matched
[perdir /data/www/some-app/] rewrite 'fun' -> 'index.php/fun'
[perdir /data/www/some-app/] add per-dir prefix: index.php/fun -> /data/www/some-app/index.php/fun
[perdir /data/www/some-app/] strip document_root prefix: /data/www/some-app/index.php/fun -> /some-app/index.php/fun
[perdir /data/www/some-app/] internal redirect with /some-app/index.php/fun [INTERNAL REDIRECT]
[authz_core:debug] mod_authz_core.c(809): AH01626: authorization result of Require all granted: granted
[authz_core:debug] mod_authz_core.c(809): AH01626: authorization result of <RequireAny>: granted
[perdir /data/www/some-app/] add path info postfix: /data/www/some-app/index.php -> /data/www/some-app/index.php/fun
[perdir /data/www/some-app/] strip per-dir prefix: /data/www/some-app/index.php/fun -> index.php/fun
[perdir /data/www/some-app/] applying pattern '^(.*)$' to uri 'index.php/fun'
[perdir /data/www/some-app/] RewriteCond: input='index.php/fun' pattern='!^(index.php)' => not-matched
[perdir /data/www/some-app/] pass through /data/www/some-app/index.php
[proxy:debug] mod_proxy.c(1228): AH01143: Running scheme unix handler (attempt 0)
[proxy_fcgi:debug] mod_proxy_fcgi.c(913): AH01076: url: fcgi://xyz//data/www/some-app/index.php proxyname: (null) proxyport: 0
[proxy_fcgi:debug] mod_proxy_fcgi.c(920): AH01078: serving URL fcgi://xyz//data/www/some-app/index.php
[proxy:debug] proxy_util.c(2156): AH00942: FCGI: has acquired connection for (*)
[proxy:debug] proxy_util.c(2209): AH00944: connecting fcgi://xyz//data/www/some-app/index.php to xyz:8000
[proxy:debug] proxy_util.c(2246): AH02545: fcgi: has determined UDS as /var/run/php-fpm-xyz.sock
[proxy:debug] proxy_util.c(2418): AH00947: connected //data/www/some-app/index.php to httpd-UDS:0
[proxy:debug] proxy_util.c(2786): AH02823: FCGI: connection established with Unix domain socket /var/run/php-fpm-xyz.sock (*)
[authz_core:debug] mod_authz_core.c(809): AH01626: authorization result of Require all granted: granted
[authz_core:debug] mod_authz_core.c(809): AH01626: authorization result of <RequireAny>: granted
[proxy_fcgi:error] AH01071: Got error 'Primary script unknown\n'
[proxy:debug] proxy_util.c(2171): AH00943: FCGI: has released connection for (*)
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
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