简体   繁体   English

如何使用 .htaccess 重定向页面但也将其用作 W3 包括

[英]How to use .htaccess to redirect a page but also use it as a W3 include

I am building an html site using the include method shown here from w3schools我正在使用 w3schools 此处显示的包含方法构建 html 站点

https://www.w3schools.com/howto/howto_html_include.asp https://www.w3schools.com/howto/howto_html_include.asp

the site basically looks like the following该网站基本上如下所示

<div w3-include-html="navigation.html"></div>

<body>
</body>

<div w3-include-html="footer.html"></div>

It works great when you're on the right pages of the website but if I navigate directly to navigation.html it displays a terrible looking and useless page.当您在网站的正确页面上时效果很好,但是如果我直接导航到 navigation.html,它会显示一个看起来很糟糕且无用的页面。

I tried to redirect it using the .htaccess file with something like the following so a user could gracefully land on a page that says sorry this doesn't exist, click here blah blah...我尝试使用类似以下内容的 .htaccess 文件重定向它,以便用户可以优雅地登陆一个页面,上面写着对不起,这不存在,点击这里等等等等。

Redirect /navigation.html /page-not-found.html重定向 /navigation.html /page-not-found.html

however that ends up causing the navigation bar on all my real pages to display the html within the page-does-not-exist.html page.但是,这最终导致我所有真实页面上的导航栏在 page-does-not-exist.html 页面中显示 html 页面。

Is there an easy way to use .htaccess to redirect a user who is directly typing in www.example.com/navigation.html to www.example.com/page-not-found.html while still allowing my other pages to 'include' it normally?有没有一种简单的方法可以使用 .htaccess 将直接输入www.example.com/navigation.html的用户重定向到www.example.com/page-not-found.ZFC35FDC70D5FC69D269883A822C仍然允许其他页面包含 A正常吗?

Thank you for any help you can offer.感谢您提供的任何帮助。

This method uses JavaScript ( XMLHttpRequest object, ie. "AJAX") to make an HTTP request for the document on the server. This method uses JavaScript ( XMLHttpRequest object, ie. "AJAX") to make an HTTP request for the document on the server. Ordinarily, this is no different (from a server perspective) to a direct request that the client might make by typing the URL for the file directly.通常,这与客户端可能通过直接为文件键入 URL 发出的直接请求没有什么不同(从服务器的角度来看)。

You need to make the JavaScript request different .您需要使 JavaScript 请求不同

You can add a custom HTTP request header (or override the User-Agent string) to the JavaScript request and check for this header using mod_rewrite in .htaccess on the server to send an alternative response if the file is requested directly, ie. You can add a custom HTTP request header (or override the User-Agent string) to the JavaScript request and check for this header using mod_rewrite in .htaccess on the server to send an alternative response if the file is requested directly, ie. when the header is not present, or set as expected.当 header 不存在或按预期设置时。

For example, based on the code from w3schools, add a second line after creating the XMLHttpRequest object:例如,根据 w3schools 的代码,在创建XMLHttpRequest object 之后添加第二行:

xhttp = new XMLHttpRequest();
xhttp.setRequestHeader('User-Agent','XMLHTTP');

This sets the User-Agent header in the JS request to XMLHTTP .这会将 JS 请求中的User-Agent header 设置为XMLHTTP We can then check for this using mod_rewrite and serve a 404 if not set.然后,我们可以使用 mod_rewrite 进行检查,如果未设置,则提供 404。

However, you should not "redirect" to the page-not-found.html script.但是,您不应“重定向”到page-not-found.html脚本。 Set this as a custom 404 error document instead and allow Apache to serve this.将此设置为自定义 404 错误文档,并允许 Apache 提供此服务。

For example:例如:

ErrorDocument 404 /page-not-found.html

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} !=XMLHTTP
RewriteRule ^navigation\.html$ - [R=404]

Any request for /navigation.html where the User-Agent string is not XMLHTTP is served the custom 404 error document, ie. User-Agent字符串不是XMLHTTP/navigation.html的任何请求都将提供自定义 404 错误文档,即。 /page-not-found.html . /page-not-found.html So, only requests from your script are permitted.因此,只允许来自您的脚本的请求。

NB: This is not a security feature, it simply prevents anyone who casually types the URL into the browser from seeing the content (including search engines).注意:这不是一项安全功能,它只是防止随便在浏览器中键入 URL 的人看到内容(包括搜索引擎)。


Aside:在旁边:

 <div w3-include-html="navigation.html"></div> <body> </body> <div w3-include-html="footer.html"></div>

It looks from this that you are including HTML elements outside of the HTML body ?由此看来,您在 HTML body之外包含了 HTML 元素? This isn't valid HTML.这不是有效的 HTML。

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

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