Rewrite rules cms phpwcms not working

I try to convert apache rewrite rules for CMS phpwcms (www.phpwcms.de),
but
with the online converter tools and some adjustments recommended at the
nginx wiki I still struggle with that.

That’s my configuration
Server
Debian Whezzy
nginx v 1.6.2
php5-fpm 5.5.20-1~dotdeb.1 (fpm-fcgi)

Rewrite Rule Apache
RewriteEngine on
RewriteBase /
RewriteRule ^
(track|include|img|template|picture|filearchive|content|robots.txt|favicon.ico)($|/)

  • [L]
    RewriteRule ^ index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^
    ([0-9]+).([0-9]+).([0-9]+).([0-9]+).([0-9]+).([0-9]+).html$
    /index.php?id=$1,$2,$3,$4,$5,$6&%{QUERY_STRING}
    RewriteRule ^ (.+).html$ /index.php?$1&%{QUERY_STRING}

Rewrite should do for example this:
http://hometest.home.local/home_de.html
http://hometest.home.local/index.php?home_de

CMS without rewrite works fine.

Tests with the converted rewrite rules where not working and more
confused
then rewriting with the try_files option.
So I switched to test with try_files. Examples for wordpress or drupal
where
not working too.

Now I’m at this stage. I believe the main problem is the “QUERY_STRING”,
but
I have no idea how that get rendered.

Nginx Site Config /etc/nginx/sites-available/default :
listen 80 default_server;
server_name hometest.home.local;
root /data/www/vhosts/default/public_html;
location / {
try_files $uri /index.php;
}
location ~ .php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
rewrite_log on;
access_log /data/www/vhosts/default/logs/access.log;
error_log /data/www/vhosts/default/logs/error.log debug;
}

That’s what the logfile shows.

Nginx Error Log:
[debug] 6102#0: *1 http run request: “/index.php?”
[debug] 6102#0: *1 http upstream check client, write event:1,
“/index.php”
[debug] 6102#0: *1 http upstream recv(): -1 (11: Resource temporarily
unavailable)

[error] 6102#0: *1 FastCGI sent in stderr: "PHP message: PHP Notice:
Use of
undefined constant Y - assumed ‘Y’ in
/data/www/vhosts/default/public_html/include/inc_front/front.func.inc.php(2287)
: eval()'d code on line 1

** front.func.inc.php(2287)**
function include_int_phpcode($string) {
// return the PHP code
$s = html_despecialchars($string[1]);
$s = str_replace(‘
’, “\n”, $s);
$s = str_replace(‘
’, “\n”, $s);
ob_start();
eval($s.“;”);
return ob_get_clean();
}

I hope someone can help me with this. The phpcwms forum couldn’t give me
an
answer so fare.

Thank you
Daniel

Posted at Nginx Forum:

On Sun, Feb 15, 2015 at 09:02:05AM -0500, dansch8888 wrote:

Hi there,

I’ve not tried to use phpwcms. But from the apache config and your
description, I think that the following provides some of the information
to the php interpreter that it wants.

(track|include|img|template|picture|filearchive|content|robots.txt|favicon.ico)($|/)

  • [L]

I think that say “anything that matches that pattern should be served
as a plain file, and not given to the php interpreter”. But there are
also .htaccess files in some of those directories.

So, after you are happy that the thing you want to be working is working
on your test system, add entries like

location ^~ /track/ {}
location ^~ /filearchive/ { deny all; }

to your server{} block, according to what you want, before you make it
public.

http://hometest.home.local/index.php?home_de
I think that the above rewrites to
http://hometest.home.local/index.php?home_de&

If the difference (the extra &) does not matter, then all is good.

At http level, add

map $request_uri $bit_of_qs {
default “”;
~/(?P.*).html $name;
}

Then at server level include

server {

if ($request_uri ~ /(\d+)\.(\d+)\.(\d+)\.(\d+)\.(\d+)\.(\d+)\.html) 

{
set $bit_of_qs “id=$1,$2,$3,$4,$5,$6”;
}

location / {
  try_files $uri $uri/ @index.php;
}

location ~ \.php$ {
  fastcgi_pass unix:/var/run/php5-fpm.sock;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location @index.php {
  fastcgi_pass unix:/var/run/php5-fpm.sock;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root/index.php;
  fastcgi_param QUERY_STRING $bit_of_qs&$query_string;
}

}

Depending on your fastcgi server, you may want the “include” line after
the “fastcgi_param” lines.

Now I’m at this stage. I believe the main problem is the “QUERY_STRING”, but
I have no idea how that get rendered.

The above sets QUERY_STRING to what I think is the same thing that your
apache config sets it to.

I hope someone can help me with this. The phpcwms forum couldn’t give me an
answer so fare.

I don’t know if the above will let everything work – I’d expect that
it would not – but it should push you in the right direction.

(For example: it is not immediately clear to me what should happen to
a request for /a/1.2.3.4.5.6.html – probably the above configuration
does not do what is wanted there.)

Good luck with it,

f

Francis D. [email protected]

On Tue, Feb 17, 2015 at 06:26:59PM -0500, dansch8888 wrote:

Hi there,

I tried your examples and they work very well.
In the next days I will test it more extensive.

Good to hear, thanks.

(track|include|img|template|picture|filearchive|content|robots.txt|favicon.ico)($|/)

  • [L]
    I think that say “anything that matches that pattern should be served
    as a plain file, and not given to the php interpreter”. But there are
    also .htaccess files in some of those directories.

Yes, that’s true, but isn’t it also that at the apache server this folders
and files are just excluded for rewrite?

That sounds correct. I stated it badly.

“Do not rewrite, and therefore do not fall through to /index.php, for
these requests.”

They are handled by whatever else the apache config says.

if ($request_uri ~ /(\d+).(\d+).(\d+).(\d+).(\d+).(\d+).html) {
set $bit_of_qs “id=$1,$2,$3,$4,$5,$6”;
}

This is comming from some old versions of the CMS I believe, were not always
aliases were set up.
At the moment, when you create a article, the CMS sets an dummy alias by
default.
In the “old” time, there were urls like this index.php?id=0.0.0.0.0.0
Now there are normaly such urls index.php?dummyalias only.

If that part isn’t needed, then so much the better.

All nginx cares about is: for incoming requests, how should they be
handled?

location / {
try_files $uri $uri/ @index.php;
}

Is there a need for the $uri/?

The apache config seemed not to do the rewrite (= /index.php fallback)
if the request was for something that was recognised as an existing
directory. This is the same.

I do not know if it is needed in your implementation.

I had read this is for subfolder only. This CMS need the rewrite just for
the root folder and index.php.

It was not clear to me how a request for /dir/file.html should be
handled,
depending on the existence of “dir” and “file.html”.

If that kind of request matters in the cms, you should probably test
that it does what you want.

Good luck with it,

f

Francis D. [email protected]

Hi Francis,

thank you for this really good explanation.

I tried your examples and they work very well.
In the next days I will test it more extensive.

(track|include|img|template|picture|filearchive|content|robots.txt|favicon.ico)($|/)

  • [L]
    I think that say “anything that matches that pattern should be served
    as a plain file, and not given to the php interpreter”. But there are
    also .htaccess files in some of those directories.

Yes, that’s true, but isn’t it also that at the apache server this
folders
and files are just excluded for rewrite?
In my config I set folders with .htaccess only at the moment.
location ^~ /config/ { deny all; }
location ^~ /filearchive/ { deny all; }
location ^~ /track/ { deny all; }
location ^~ /upload/ { deny all; }

if ($request_uri ~ /(\d+).(\d+).(\d+).(\d+).(\d+).(\d+).html) {
set $bit_of_qs “id=$1,$2,$3,$4,$5,$6”;
}

This is comming from some old versions of the CMS I believe, were not
always
aliases were set up.
At the moment, when you create a article, the CMS sets an dummy alias by
default.
In the “old” time, there were urls like this index.php?id=0.0.0.0.0.0
Now there are normaly such urls index.php?dummyalias only.

location / {
try_files $uri $uri/ @index.php;
}

Is there a need for the $uri/?
I had read this is for subfolder only. This CMS need the rewrite just
for
the root folder and index.php.

Thank you so fare for this quick help.

Daniel

Posted at Nginx Forum:

Hi Francis,

after some testing I use this rules now. These are working fine with my
environment.

Nginx Site Config /etc/nginx/sites-available/default :
map $request_uri $bit_of_qs {
default “”;
~/(?P.).html $name;
}

server {

location ^~ /config/phpwcms/ { deny all; }
location ^~ /filearchive/ { deny all; }
location ^~ /upload/ { deny all; }
location ~ /. { access_log off; log_not_found off; deny all; }
location / {
try_files $uri @phpwcms;
}
location @phpwcms {
fastcgi_pass unix:/var/run/php5-fpm/default.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param QUERY_STRING $bit_of_qs&$query_string;
}
location ~
^.+.php$ { return 404; }

}

I hope this rules will catch all the following needs.

  1. Deny access to folders /config/phpwcms, /filearchive, /upload
  2. Deny all hidden files
  3. Rewrite /index.php…
  4. Ignore and do not show any other php file at root folder or any other
    sub
    folder to the internet

Is there something that should be improved?

One thing that is still happen is the following error message. No idea
which
“undefined constant Y” means.

Nginx Error Log
[error] 2798#0: *14 FastCGI sent in stderr: “PHP message: PHP Notice:
Use
of undefined constant Y - assumed ‘Y’ in
/xxx/xxx/xxx/xxx/public_html/include/inc_front/front.func.inc.php(2287)
:
eval()'d code on line 1” while reading response header from upstream,
client: 192.x.x.x, server: hometest.home.local, request: “GET
/home_de.html
HTTP/1.1”, upstream: “fastcgi://unix:/var/run/php5-fpm/default.sock:”,
host:
“hometest.home.local”, referrer: “https://hometest.home.local/

Thanks
Daniel

Posted at Nginx Forum:

Hi Francis,
I got a way more familar now with this nginx config files. It works fine
for
me.

The only thing that I still not get figured out, is this FastCGI “Y”
error.
It doesn’t happen at the old webserver which is an apache/fastcgi
environment, but I cannot play around with this, because it is just a
webspace.

At the line 2287 there is this code
eval($s.“;”);

and
“FastCGI sent in stderr: “PHP message: PHP Notice: Use of undefined
constant Y - assumed ‘Y’ in /xxx/front.func.inc.php(2287) : eval()'d
code on
line 1” while reading response header from upstream,…”

at line 1 in this file I can not see any Y?

I still hope the developer can give me some informations.

Daniel

Posted at Nginx Forum:

On Mon, Feb 23, 2015 at 01:51:41PM -0500, dansch8888 wrote:

Hi there,

after some testing I use this rules now. These are working fine with my
environment.

That’s good to hear.

fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param QUERY_STRING $bit_of_qs&$query_string;
}
location ~* ^.+.php$ { return 404; }

I hope this rules will catch all the following needs.

  1. Deny access to folders /config/phpwcms, /filearchive, /upload

Yes.

  1. Deny all hidden files

Filenames that start with a dot, yes.

  1. Rewrite /index.php…

I’m not sure what exactly you mean by that. A request for /file that
does not exist will be handled by the fastcgi server processing
index.php.

  1. Ignore and do not show any other php file at root folder or any other sub
    folder to the internet

any php request. So if you ask for /index.php directly, you will get
404.

Is there something that should be improved?

If it shows what you want and hides what you want, it is probably right.

A minor thing is that the ^.+ in the final regex location is probably
unnecessary.

One thing that is still happen is the following error message. No idea which
“undefined constant Y” means.

Nginx Error Log
[error] 2798#0: *14 FastCGI sent in stderr: "PHP message: PHP Notice: Use
of undefined constant Y - assumed ‘Y’ in
/xxx/xxx/xxx/xxx/public_html/include/inc_front/front.func.inc.php(2287) :

That sounds like a php error. Does the same thing happen when the
application is run in its “native” environment of apache/mod_php? Or
in the apache/fastcgi environment? If not, you could investigate the
differences.

I suspect you’re more likely to get useful on the application mailing
list.

Good luck with it,

f

Francis D. [email protected]

I found the problem with the Y.
It was this “[PHP] echo date(Y);[/PHP]” code in my CMS content, where
the Y
not was quoted ‘Y’.
I changed it to “[PHP] echo date(‘Y’);[/PHP]” and all went fine now.
The [PHP] is just a replacement tag of the CMS, where you can render PHP
code in the system.

All works fine now.
Thanks

Posted at Nginx Forum: