简体   繁体   中英

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?

http://myserver/file20180401.csv http://myserver/file20180402.csv

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]
</Directory>`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM