Languages
[Edit]
EN

apache2 / .htaccess - run php script if file does not exist

9 points
Created by:
JustMike
27910

In this short article we would like to show you how configure .htaccess to execute some alernative script when file indicated in URL doesn't exist on apache2 server.

Quick solution (.htaccess file):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /script.php [L]

 

Web site details

Web site structure:

/var/www/html
          ÔöťÔöÇÔöÇ .htaccess
          ÔöťÔöÇÔöÇ file1.txt
          ÔöťÔöÇÔöÇ file2.txt
          ÔööÔöÇÔöÇ script.php

.htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /script.php [L]

file1.txt file:

file1.txt content ...
file1.txt content ...
file1.txt content ...

file2.txt file:

file2.txt content ...
file2.txt content ...
file2.txt content ...

script.php file:

<?php

	echo "Script executed ...";

?>

Screenshots: 

Some unexisting file on apache2 server - script.php executed instead.
Some unexisting file on apache2 server - script.php executed instead.
Existing file1.txt loaded - apache2 server.
Existing file1.txt loaded - apache2 server.
Existing file2.txt loaded - apache2 server.
Existing file2.txt loaded - apache2 server.
script.php executed as index.php - apache2 server.
script.php executed as index.php - apache2 server.

 

Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join