2

I am working in a project where it needs to serve research papers and documents, so while serving the file it need to check few pass, if everything seems good then it should forward the file(research papers).

It would be easy to read the file and stream it using PHP but we need to use the apache xsendfile to serve the files.

The application works differently, however I have made a small project like this so that we can re-produce the problem.

All requests go to an IP address, and using a .htaccess file, it will redirect the request to index.php file.

Here is the htaccess file:

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

In the index.php it would decide which file to serve. Here is the code of index.php:

<?php

  $file = 
    __DIR__ 
    . '/scholar' 
    . $_SERVER[ 'REQUEST_URI' ] 
    . 'index.html';

  // echo $file;
  // exit();

  header("Content-type: " . mime_content_type( basename( $file ) ));
  header("X-Sendfile: " . $file );

In the $file variable it will generate the full path of the requested research paper.

  • Requested URL: http://dev.edu/project-1/
  • Generated File Path: /var/www/html/scholar/project-1/index.html

Here the file path is valid, but the file was not found!

enter image description here

Though the error message indicates that the index.php file was not found but if you uncomment this two line,

  // echo $file;
  // exit();

you will be able to see that the index.php file was actually used by apache and it would print the full valid path of the file!

I have tried my best to let you understand the problem, i can't really understand whats the issue that causing this.

Edit: folder tree of /var/www/html/

enter image description here

Edit: xsendfile is installed and enabled

enter image description here

rakibtg
  • 5,521
  • 11
  • 50
  • 73
  • edit your .htaccess to point to the folder using `RewriteRule ^scholar$ /scholar/project-1/index.html` . i am assuming that /scholar is in the root web directory – JamesBond Jul 24 '18 at 18:23
  • found this : https://stackoverflow.com/questions/13672998/rewrite-rule-for-subfolder?rq=1 – JamesBond Jul 24 '18 at 18:27
  • don't you have `XSendFilePath` apache option set somewhere? – Alex Jul 24 '18 at 18:30
  • @Alex I have tried to add this in the htaccess file, but it would fail too. – rakibtg Jul 24 '18 at 18:32
  • why do you think it will fail? – Alex Jul 24 '18 at 18:33
  • @JamesBond Thanks taking a look at this. By adding a new rule showing this message: "The requested URL /project-1/ was not found on this server." – rakibtg Jul 24 '18 at 18:33
  • @Alex Adding this to the htaccess file `XSendFilePath ./` fives an error. "Internal Server Error" – rakibtg Jul 24 '18 at 18:34
  • maybe if you try to let .htacces do all the work instead of the index.php. remove everything from your .htaccess file and simply try adding this `RewriteEngine on RewriteRule ^project-1$ /scholar/project-1/index.html` – JamesBond Jul 24 '18 at 18:38
  • `XSendFilePath /var/www/html/scholar` – Alex Jul 24 '18 at 18:38
  • @Alex adding this `XSendFilePath /var/www/html/scholar` gives the same error: "Internal Server Error " – rakibtg Jul 24 '18 at 18:40
  • it gives where? on the page? or when restart apache? usually `Internal Server Error ` is about php errors. change php to `die('test');` to understand where the error is? – Alex Jul 24 '18 at 18:42
  • @Alex I have added a check in the php file, `if ( !file_exists( $file ) ) exit('File not found');` but still the same error! It seems that when i set the `XSendFilePath` in the htaccess the server goes down! Without the `XSendFilePath` in the htaccess file it properly check if the file exists according to above check. Thanks – rakibtg Jul 24 '18 at 18:48
  • Are you sure you have *xsendfile* installed and enabled then? – Alex Jul 24 '18 at 18:51
  • @Alex Yes https://i.stack.imgur.com/mScoG.png – rakibtg Jul 24 '18 at 18:53
  • the only thing to check then: permissions on folder /var/www/html I guess that should be www-data or apache2. it make no sense why server crash when you add `XSendFilePath` param – Alex Jul 24 '18 at 18:55
  • Yes, changed the permission: `drwxrwxrwx 6 www-data www-data 4096 Jul 25 00:38 html` but still the same error – rakibtg Jul 24 '18 at 19:00
  • `drwxrwxr-x 4 www-data www-data 4096 Jul 24 23:42 scholar` – rakibtg Jul 24 '18 at 19:01
  • @Alex @JamesBond i was able to make it work using this url pattern: `http://dev.edu/index.php/project-1/` !! Then in the `index.php` i needed to modify the file path like this: `$file = str_replace( '/index.php', '', $file );` That seems to be working i dont know how, again it has another issue, that the index.php name in the URL! Is it possible to get rid of the `/index.php/` as uri segment? – rakibtg Jul 24 '18 at 19:10
  • but in your OP you said `Requested URL: http://dev.edu/project-1/ Generated File Path: /var/www/html/scholar/project-1/index.html` So that was just misinformation. Check what do you post next time :-) – Alex Jul 24 '18 at 19:14
  • No that was correct too, going to `http://dev.edu/index.php/project-1/` will generate the file path as: `/var/www/html/scholar/index.php/project-1/index.html` (No file exists in this path) Thats why in the `index.php` file the file path requires modifications, that deletes the `index.php/` from the URL: `$file = str_replace( '/index.php', '', $file );` – rakibtg Jul 24 '18 at 19:17
  • `http://dev.edu/project-1/` => `/var/www/html/scholar/project-1/index.html` **Valid File Path** and `http://dev.edu/index.php/project-1/` => `/var/www/html/scholar/index.php/project-1/index.html` **Invalid File Path** – rakibtg Jul 24 '18 at 19:24

1 Answers1

0

Having .htaccess do all the work for you:

RewriteEngine on
RewriteRule ^project-1$ /scholar/project-1/index.html
RewriteRule ^project-2$ /scholar/project-2/index.html

if you enter into the url http://localhost/project-1 it will direct you to project-1's folder

i just tested it now.

if you would like to include the trailing slash into the url to avoid 404 then simply change

    RewriteEngine on
    RewriteRule ^project-1/$ /scholar/project-1/index.html
    RewriteRule ^project-2/$ /scholar/project-2/index.html
JamesBond
  • 312
  • 2
  • 17
  • Thanks for the answer, there could be more than few hundreds projects and documents so cant map it in a htaccess file – rakibtg Jul 24 '18 at 18:42
  • okay, will try and find a better solution, got some time on my hands – JamesBond Jul 24 '18 at 18:43
  • @rakibtg if you just need some kind of redirect - you don't need to use SendFile header at all. – Alex Jul 24 '18 at 18:45
  • @Alex thats exactly what i was thinking, you could make index.php map out all the project folders and display them on index.php then simply select which one you would like to make active etc – JamesBond Jul 24 '18 at 18:46
  • @rakibtg have you tried adding `XSendFilePath /path/to/files/directory' to the .htaccess – JamesBond Jul 24 '18 at 18:47
  • @Alex Yes! The files could be huge in size, and before sending the files to the users we need to do some check. The file path would remain same, so after doing the check if everything passed, it should send the file to the user. – rakibtg Jul 24 '18 at 18:51