简体   繁体   中英

Using .htaccess to redirect an .html to a .php

I have 5 wordpress sites that all use the same encoded template. That encoded template (which I can't change) calls on a file called "2012.orange.html" with some basic html layout code. 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/".

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.

I did something similar with my 'robots.txt' using the following code, and it works well:

# 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:

# 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:

<?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?

.htaccess is only used in the HTTP request to redirect an url . Once the script is running, including templates is not incluenced in any way by .htaccess, since you're including files, not urls.

can you type 2012_orange.php file path from the public folder?

Your code seems working fine for 2012_orange.php in the root directory,

if you having 2012_orange.php file inside another folder you have to change path like home/2012_orange.php

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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