douxian9060 2014-03-20 16:20
浏览 43
已采纳

HTML:显示来自单独PHP文件的字符串

I've searched all over the internet and can't seem to find a solution to my problem. I want to be able to "call" a php file from an html file and display the string returned:

html_ONLY_file.html

...
<h1>GetHeader.php?type=main</h1>
...

GetHeader.php

if($_GET['type'] == 'main')
   print 'Some header to display'; //or echo 'Some...';

exit;

I've done this for images, img src="image.php?file=file.jpg", where image.php does a header(...) and readfile(...) return but I do not know how to do this for simple text. I'm not looking for DOM or anything too involved, if I have to I will. I want to know if there is a simple solution. It's generating the html side that I'm lost on.

In case you want to know, I am doing this because I once used a <#virtual include=...> to call for a php file from my .shtml file. Well, the hosting company decided to change mod_security and now I cannot include any php files (I've already tried everything). Including html files works fine and so I am changing this part of the website around so I don't have to rename files because the site is for a small business that is now ranked highly on Google for its geographical area. If I change the file names, shtml to php, then I believe the Google ranking drops (don't comment on this part unless you are damn sure you are 100% correct).

  • 写回答

1条回答 默认 最新

  • drgawfsf1069 2014-03-20 16:59
    关注

    If you can edit the .htaccess file you can add a line in your .htaccess file that will mean HTML files will be parsed as if they were PHP.

    If your server is running PHP as an apache module:

    AddType application/x-httpd-php .html .htm
    

    If your server is running PHP as CGI:

    AddHandler application/x-httpd-php .html .htm 
    

    Source: http://www.besthostratings.com/articles/php-in-html-files.html

    Once you've added that your html_ONLY_file.html could look like:

    <h1><?php print "some header"; ?></h1>
    

    And it would function just as if it were a .php file.


    Alternatively, you could convert all your files to .php and add redirects into the .htaccess file like so:

    rewriteRule ^somefile.shtml$ somefile.php [R=301,L]
    rewriteRule ^another-file.shtml$ another-file.php [R=301,L]
    

    This effectively says to your server "whenever a user requests somefile.shtml, act as if they requested somefile.php instead". The R=301 is the most important part with regards to Google rankings. This tells Google (and anyone who requests the .shtml file) that it's permanently moved to the new location somefile.php. This transfers all / almost all of the ranking power from the old location to the new location.

    Source: http://moz.com/learn/seo/redirection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题