2

I have a php function which detects $_SERVER["PATH_INFO"] and then explodes all the params after index.php. And all urls are rewritten using .htaccess.

Here's the code for reference:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

The problem is my rewriting and my php function work fine locally, but on my test environment server, the function doesn't return $_SERVER["PATH_INFO"]without having index.php in my url.

For example:

if I go to example.com/index.php/this/is/a/test, the function will give me an array like this: {this, is, a, test}

but if I go to example.com/this/is/a/test rewrites to index.php but my function gives me nothing.

I've already seen the answers here, but I think this is more of a server config issue. The server is current running on CentOS, with a OVH RELEASE 3 config (similar to cPanel, ISP Config). web packages are Apache2 and php5.

Appreciate any help coming this way. ✌️

Community
  • 1
  • 1

2 Answers2

3

PATH_INFO shows the part after a script name.

When the request is /this/is/a/test, there is no script and therefore no PATH_INFO.

You can work around this by using REQUEST_URI, which would include index.php in the first case, or add an explicit query string, e.g.

RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]

Update

It turns out, that I was wrong. I finally came around testing this with a minimal script

<?php
echo "PATH_INFO=", $_SERVER['PATH_INFO'];

and it shows the PATH_INFO with either index.php/test or with just /test. This is on a Ubuntu 16.04 system with a standard installation.

So to find out what's really going on, one must look into Apache's error log for more details.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0

Actually, I don't know why this is happening specifically with apache2 server in a Linux environment(Me too faced the same issue). As I understand, your code is working perfectly well on a cloud web server without using of "index.php" written on your URL like http://www.yourdomain.com/login/ but not working like http://127.0.0.1/login/ and should work like http://127.0.0.1/index.php/login for the MVC architecture with .htaccess.

You can overcome this situation in your test environment by using the inbuilt PHP server in the test environment location. For noobs, If you open your terminal on the location "/var/www/html/" and type sudo php -S 127.0.0.1:5000 the server will start on the address http://127.0.0.1:5000. Now the rewriting of URLs using the same .htaccess will work without using the "index.php" in URL like http://127.0.0.1:5000/login/.