1

Hi I have a LEMP Server and I have used .htaccess file as bellow to redirect empty directories to index.php but the problem is that it's not executing the code in the index.php it's just printing the Array()

.htaccess file

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

nginx server configuration

server {
        listen 80;
        root /storage/html/stream;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name stream.example.com;
        include snippets/phpmyadmin.conf;

        location / {
                try_files $uri $uri/ =404 /rest/index.php?$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }

}

phpMyAdmin location that I have included it in my server block

location /phpmyadmin {
    root /storage/html/stream/;
    index index.php index.html index.htm;
    location ~ ^/phpmyadmin/(.+\.php)$ {
        try_files $uri =404;
        root /storage/html/stream/;
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

    location ~ ^/phpmyadmin/(doc|sql|setup)/ {
           deny all;
    }

    location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        root /storage/html/stream/;
    }
}

And this is my index.php file

<?php

print_r($_GET);

require_once("app/config.php");

require_once("app/core/core.php");

require_once( "app/core/router.php");

This is the output for http://stream.example.com/rest/?path=video/server

Array
(
    [path] => video/server
)
{"authorization":0,"error":1,"result":0,"message":"Token is invalid."}

And this is the output for http://stream.example.com/rest/video/server

Array ( ) 

but they both should give me this output:

Array ( [path] => video/server/ )

can anyone help me with this problem ?

  • 2
    `.htaccess` is an Apache configuration file and you appear to be using Nginx. – Richard Smith May 28 '21 at 17:18
  • "the problem is that it's not executing the code in the index.php it's just printing the Array()" - It IS executing the code in `index.php`, otherwise it wouldn't "print Array()". You just aren't passing the expected information to your script. `$args` is empty. `path=$uri` is "closer" to the sort of thing you are expecting. `'=404` in the `try_files` directive would seem to be redundant (and in the wrong order). – MrWhite May 28 '21 at 17:28

1 Answers1

1

Finally I have transfered the index.php file to root of the server and fixed this issue by adding index.php?$uri to my try_files and YeaP the .htaccess file wasn't working at all

But I have a question that if I want to have my index.php in a subdirectory what should I do ?

By the way the name of this type of configuration is Front Controller and you can search about it by this name

Thanks

  • "have my `index.php` in a subdirectory" - there shouldn't be a problem with that; what problem are you having? – MrWhite May 30 '21 at 13:06