So, I have a PHP script that we’re going to call template.php
, it has all the intelligence I need.
Today I have several files in a folder with names like name1.php
, name2.php
and others.
All these files have the same content: <?php include('template.php') ?>
.
The intelligence of template.php
uses the filename of the URL to distinguish one file from another.
The problem is that I have to create these files physically, inside the folder.
My question is (and I haven’t found anything out there): using .htaccess
can I make the server run a script based on the URL with a template?
Example:
- Assume the
template.php
file exists in the folder. - No other files are present in the folder.
- Customer accesses
www.example.com/name1.php
orwww.example.com/name2.php
.htaccess
identifies the file and proceeds with executing the script based ontemplate.php
and the file name, such asname1.php
orname2.php
.
Can you give me an idea or point out where I can acquire more knowledge on the subject?
2
Answers
I would point all traffic to
template.php
. Then within template.php you can do your execution, then in include the requested file (if it exists).You could do error handling before or after the execution of the template.php file depending on if you want it to execute based on whether or not the requested file exists.
.htaccess would look something like:
Then your template.php
You could add a condition to check so it is not the
template.php
file and rewrite to this file otherwise e.g:Here we are sending the name of the requested file as a query string to the
template.php
file: