简体   繁体   English

如何在不将HTML更改为sHtml的情况下包含其他代码(菜单栏或边栏)

[英]How to include another code (Menu bar or Side bar) without changing your Html to sHtml

I want to include header into many webpages So I want to create a single file and include in all the pages. 我想在许多网页中包含标头,所以我想创建一个文件并包含在所有页面中。 There is one way that I found is by SSI (server side include) but it require changing extension of webpage from Html to sHtml(that I cannot do due to some restriction). 我发现一种方法是通过SSI(服务器端包含),但是它需要将网页的扩展名从Html更改为sHtml(由于某些限制,我无法做到)。

Is there any way to do this without changing extension? 有什么方法可以做到而无需更改扩展名?

Last time I checked (which was a number of years ago), with Apache, if you made the html files executable by httpd process, they would act like shtml, without having to rename them. 上次我检查(是几年前)的Apache时,如果将html文件设置为可通过httpd进程执行,则它们的作用就像shtml,而无需重命名。

I leave the security implications of doing this to your own evaluation. 我将这样做的安全性留给您自己评估。

You can use jQuery/AJAX approach 您可以使用jQuery / AJAX方法

$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});

Reference: http://api.jquery.com/jQuery.get/ 参考: http//api.jquery.com/jQuery.get/

Your best methods are PHP files with includes. 最好的方法是带有include的PHP文件。 SSI works well, but you have to have the .shtml file. SSI运作良好,但您必须拥有.shtml文件。

Non recommended methods would be using an iframe to include the header. 不建议使用的方法是使用iframe包含标头。 Javascript can also be used but it isn't good to use client side includes. 也可以使用Javascript,但是使用客户端包含不是很好。 jQuery would also be client side includes. jQuery也将是客户端包含的。

You can set .html files to use SSI but if you have restrictions this is most likely a restriction that you can not get past. 您可以将.html文件设置为使用SSI,但如果有限制,则很可能是您无法逾越的限制。 Hosted server accounts do not allow this for the most part. 托管服务器帐户在大多数情况下不允许这样做。

Apache Server docs on SSI. SSI上的Apache服务器文档。 http://httpd.apache.org/docs/current/howto/ssi.html http://httpd.apache.org/docs/current/howto/ssi.html

To permit SSI on your server, you must have the following directive either in your httpd.conf file, or in a .htaccess file: 要在服务器上允许SSI,必须在httpd.conf文件或.htaccess文件中具有以下指令:

Options +Includes This tells Apache that you want to permit files to be parsed for SSI directives. Options + Includes这告诉Apache您想允许对文件进行SSI指令解析。 Note that most configurations contain multiple Options directives that can override each other. 请注意,大多数配置包含多个可以相互替代的Options伪指令。 You will probably need to apply the Options to the specific directory where you want SSI enabled in order to assure that it gets evaluated last. 您可能需要将“选项”应用到要启用SSI的特定目录,以确保最后评估它。

Not just any file is parsed for SSI directives. 不仅为SSI指令解析任何文件。 You have to tell Apache which files should be parsed. 您必须告诉Apache应该解析哪些文件。 There are two ways to do this. 有两种方法可以做到这一点。 You can tell Apache to parse any file with a particular file extension, such as .shtml, with the following directives: 您可以使用以下指令告诉Apache解析具有特定文件扩展名的任何文件,例如.shtml:

AddType text/html .shtml AddOutputFilter INCLUDES .shtml One disadvantage to this approach is that if you wanted to add SSI directives to an existing page, you would have to change the name of that page, and all links to that page, in order to give it a .shtml extension, so that those directives would be executed. AddType text / html .shtml AddOutputFilter包含.shtml这种方法的一个缺点是,如果要向现有页面添加SSI指令,则必须更改该页面的名称以及该页面的所有链接,以便给它一个.shtml扩展名,以便执行这些指令。

The other method is to use the XBitHack directive: 另一种方法是使用XBitHack指令:

XBitHack on XBitHack tells Apache to parse files for SSI directives if they have the execute bit set. XBitHack上的XBitHack告诉Apache如果设置了执行位,则为SSI指令解析文件。 So, to add SSI directives to an existing page, rather than having to change the file name, you would just need to make the file executable using chmod. 因此,要将SSI指令添加到现有页面中,而不必更改文件名,只需使用chmod使文件可执行。

chmod +x pagename.html A brief comment about what not to do. chmod + x pagename.html关于不执行操作的简短说明。 You'll occasionally see people recommending that you just tell Apache to parse all .html files for SSI, so that you don't have to mess with .shtml file names. 您偶尔会看到有人建议您只告诉Apache为SSI解析所有.html文件,这样就不必弄乱.shtml文件名。 These folks have perhaps not heard about XBitHack. 这些人也许还没有听说过XBitHack。 The thing to keep in mind is that, by doing this, you're requiring that Apache read through every single file that it sends out to clients, even if they don't contain any SSI directives. 要记住的事情是,这样做,您需要Apache读取它发送给客户端的每个文件,即使它们不包含任何SSI指令。 This can slow things down quite a bit, and is not a good idea. 这会使事情变慢很多,不是一个好主意。

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

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