MikeKulls MikeKulls - 5 months ago 5x
PHP Question

Get apache/php to give a response to any URL

I've tried searching but can't find anything. I would like apache/php to be able to return a response to any URL without having to have a PHP page that exists. eg, with URLs like below I don't want to create a page for each URL, I would just like to have 1 php page that queries a database underneath. The php code would somehow get the filename as a parameter so it could make decisions on that. Is that possible?



You could use mod_rewrite. Just paste this into your .htaccess file:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?file=$1 [NC]

Then simply in your index.php file (or whichever you would like to change it to) use some simple PHP code:

$file = $_GET['file'];
echo $file;

In your example http://myserver/file20180401.csv the variable $file would be equal to file20180401.csv.

EDIT: Repeating here as didn't come out well in comment. This was the solution I ended up using as the apache docs recommend for performance reasons to place it in the httpd.conf file instead of .htaccess file:

<Directory "/var/www/html/test_transporter>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*\.csv)$ file.php?file=$1 [NC]