简体   繁体   English

使用.htaccess将.html重定向到.php

[英]Using .htaccess to redirect an .html to a .php

I have 5 wordpress sites that all use the same encoded template. 我有5个wordpress网站都使用相同的编码模板。 That encoded template (which I can't change) calls on a file called "2012.orange.html" with some basic html layout code. 那个编码模板(我无法改变)调用一个名为“2012.orange.html”的文件,其中包含一些基本的html布局代码。 I have created a "my_template.php" in my server's "/home/" directory and another "2012_orange.php" file in my template directory that "includes" "my_template.php" from the "/home/". 我在服务器的“/ home /”目录中创建了一个“my_template.php”,在我的模板目录中创建了另一个“2012_orange.php”文件,其中包含“/ home /”中的“my_template.php”。

My goal is for my "my_template.php" file in the "/home/" directory to be used to control the layout of the 5 different sites, but I can't figure out how to get the specific .html file to call the .php instead. 我的目标是使用“/ home /”目录中的“my_template.php”文件来控制5个不同站点的布局,但我无法弄清楚如何获取特定的.html文件来调用.php代替。

I did something similar with my 'robots.txt' using the following code, and it works well: 我使用以下代码对我的'robots.txt'进行了类似的操作,效果很好:

# BEGIN - Robots Rewrite
RewriteEngine on
RewriteRule ^robots\.txt$ robots.php [L]
## END - Robots Rewrite

But I can't get this to work...I am getting no html layout: 但我不能让这个工作...我没有得到HTML布局:

# BEGIN - Template Rewrite
RewriteEngine on
RewriteRule ^2012\.orange\.html$ 2012_orange.php [L]
## END - Template Rewrite

And then again, the 2012_orange.php would just contain the following include: 然后,2012_orange.php将包含以下内容:

<?php include ('/home/my_template.php'); ?>

Any idea on how to get .htaccess to redirect to the .php version of the file when the original .html is called? 有关如何在调用原始.html时将.htaccess重定向到文件的.php版本的任何想法?

.htaccess is only used in the HTTP request to redirect an url . .htaccess仅用于HTTP请求中以重定向URL Once the script is running, including templates is not incluenced in any way by .htaccess, since you're including files, not urls. 脚本运行后,.htaccess不包含任何模板,因为您包含文件,而不是网址。

can you type 2012_orange.php file path from the public folder? 你可以从公共文件夹中键入2012_orange.php文件路径吗?

Your code seems working fine for 2012_orange.php in the root directory, 您的代码似乎在根目录中的2012_orange.php正常工作,

if you having 2012_orange.php file inside another folder you have to change path like home/2012_orange.php 如果您在另一个文件夹中有2012_orange.php文件,则必须更改home/2012_orange.php路径

# BEGIN - Template Rewrite
RewriteEngine on
RewriteRule ^2012\.orange\.html$ home/2012_orange.php [L]
## END - Template Rewrite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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