如何使用htaccess来运行类似模板的php脚本,使用指向不存在文件的URL?

huangapple go评论101阅读模式
英文:

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.phpname2.php等等。
所有这些文件都具有相同的内容:<?php include('template.php') ?>

template.php的智能使用URL的文件名来区分不同的文件。

问题是我必须在文件夹内创建这些文件。

我的问题是(我还没有在其他地方找到答案):使用.htaccess,我可以让服务器根据URL运行一个基于模板的脚本吗?

示例:

  • 假设文件夹中存在template.php文件。
  • 文件夹中没有其他文件。
  • 客户访问www.example.com/name1.phpwww.example.com/name2.php
  • .htaccess识别文件并执行基于template.php和文件名(例如name1.phpname2.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: &lt;?php include(&#39;template.php&#39;) ?&gt;.

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 or www.example.com/name2.php
  • .htaccess identifies the file and proceeds with executing the script based on template.php and the file name, such as name1.php or name2.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:

&lt;IfModule mod_rewrite.c&gt;
	RewriteBase /
	RewriteRule ^template\.php$ - [L]
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule . /template.php [L]
&lt;/IfModule&gt;

Then your template.php

&lt;?php
#template.php execution code

$requested_uri = $_SERVER[&#39;REQUEST_URI&#39;];

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:

&lt;?php
$requestedTemplateFileName = $_GET[&#39;name&#39;] ?? &#39;default.php&#39;;

huangapple
  • 本文由 发表于 2023年8月11日 00:45:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定