英文:
How can I use htaccess to run a template-like php script with a url pointing to a file that doesn't exist?
问题
今天我有一个名为template.php
的PHP脚本,它包含了我所需的所有智能。
现在我在一个文件夹中有几个文件,文件名类似于name1.php
,name2.php
等等。
所有这些文件都具有相同的内容:<?php include('template.php') ?>
。
template.php
的智能使用URL的文件名来区分不同的文件。
问题是我必须在文件夹内创建这些文件。
我的问题是(我还没有在其他地方找到答案):使用.htaccess
,我可以让服务器根据URL运行一个基于模板的脚本吗?
示例:
- 假设文件夹中存在
template.php
文件。 - 文件夹中没有其他文件。
- 客户访问
www.example.com/name1.php
或www.example.com/name2.php
.htaccess
识别文件并执行基于template.php
和文件名(例如name1.php
或name2.php
)的脚本。
你能给我一个想法或指出我可以获取更多关于这个主题的知识的地方吗?
英文:
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?
答案1
得分: 2
只返回翻译好的部分:
我将所有的流量都指向template.php
。然后在template.php
中,您可以执行所需的操作,然后包含请求的文件(如果存在)。
您可以在执行template.php
文件之前或之后进行错误处理,这取决于您是否希望根据请求的文件是否存在来执行它。
.htaccess文件看起来类似于:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^template\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /template.php [L]
</IfModule>
然后是您的template.php
文件:
<?php
#template.php执行代码
$requested_uri = $_SERVER['REQUEST_URI'];
if( file_exists($requested_uri) ){
include $requested_uri;
}else{
#404或其他错误处理
}
英文:
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:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^template\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /template.php [L]
</IfModule>
Then your template.php
<?php
#template.php execution code
$requested_uri = $_SERVER['REQUEST_URI'];
if( file_exists($requested_uri) ){
include $requested_uri;
}else{
#404 or other error handling
}
答案2
得分: 1
你可以添加一个条件来检查是否不是template.php
文件,如果是的话就重写到这个文件,例如:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/templates/template\.php$ [NC]
RewriteRule ^templates/(.*)$ /templates/template.php?name=$1 [L]
在这里,我们将请求的文件名作为查询字符串发送到template.php
文件:
<?php
$requestedTemplateFileName = $_GET['name'] ?? 'default.php';
英文:
You could add a condition to check so it is not the template.php
file and rewrite to this file otherwise e.g:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/templates/template\.php$ [NC]
RewriteRule ^templates/(.*)$ /templates/template.php?name=$1 [L]
Here we are sending the name of the requested file as a query string to the template.php
file:
<?php
$requestedTemplateFileName = $_GET['name'] ?? 'default.php';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论