页内锚链接的htaccess异常

编程入门 行业动态 更新时间:2024-10-28 00:29:06
本文介绍了页内锚链接的htaccess异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在.htaccess文件中使用伪指令来清理我的网站URL。

I use directives in an .htaccess file to clean-up my website URLs.

例如,此伪指令添加了斜杠:

For instance, this directive adds a trailing slash:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ RewriteRule (.*)$ /$1/ [R=301,L]

并删除文件扩展名:

# hide .php file extensions RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] RewriteRule ^ %1 [R=301,L] # redirect .html to / RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC] RewriteRule ^ %1 [R,L] # redirect .htm to / RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.htm [NC] RewriteRule ^ %1 [R,L]

我遇到的问题是页内锚。当我创建指向页面一部分的链接时,像这样:

The problem I'm having is with in-page anchors. When I create a link to a section of page, like this:

www.mywebsite/privacy-policy#information-we-collect

系统输出以下内容:

www.mywebsite/privacy-policy/#information-we-collect

当涉及页面内锚链接时,如何调整.htaccess文件以使尾部斜杠要求例外?

How do I adjust the .htaccess file to make an exception for the trailing slash requirement when it comes to in-page anchor links?

一个相关的问题是:

尽管这两个链接都起作用:

Although both of these links work:

www.mywebsite/privacy-policy#information-we-collect www.mywebsite/privacy-policy/#information-we-collect

... 在页面内部(在本例中为隐私权政策。)。

... when they are inside the page (Privacy Policy, in this case).

URL永远不会在地址栏中进行调整以显示片段标识符(#... )。地址栏固定为:

The URL never adjusts in the address bar to show the fragment identifier (#...). The address bar stays fixed at:

www.mywebsite/privacy-policy/

通常它会调整为:

www.mywebsite/privacy-policy#information-we-collect

最后,虽然这些带有片段标识符的链接在其页面中有效,但是带有片段标识符到另一个页面的链接却不起作用。

Lastly, while these links with fragment identifiers work within their page, a link with a fragment identifier to another page, does not work.

所以,如果我m在隐私权政策页面上,所有这些链接都可以正常工作:

So, if I'm on the Privacy Policy page, all these links work fine:

www.mywebsite/privacy-policy#information-we-collect www.mywebsite/privacy-policy/#information-we-collect www.mywebsite/terms-of-service/

但这完全没有响应:

www.mywebsite/terms-of-service#limitation-of-liability

仅在服务条款页面中有效。

It only works within the Terms of Service page.

解决方案

将我的评论转换为答案。

Converting my comments to an answer.

此异常无效:

  • 因为Web服务器(或重写规则)对锚点一无所知,因为URL的一部分以开头 #完全在浏览器中处理,不会发送到Web服务器。
  • #information-we-collect 部分不会发送到Web服务器,因此为创建一个例外#... 不能真正解决问题。必须有其他方法来创建异常,例如添加前缀/后缀或伪查询参数,重写规则可以查看并对其执行操作。
  • 或者,您可以在客户端本身(即在Javascript代码内部)进行处理。
  • Because a web server (or rewrite rules) don't know anything about anchors because part of URL starting with # is completely handled in browser and is not sent to web server.
  • #information-we-collect part won't be sent to web server so creating an exception for #... won't really solve the problem. There has to be some other way to create exception e.g. adding a prefix/suffix or a dummy query parameter that rewrite rules can see and take action on.
  • Alternatively you can handle it on client side itself i.e. inside Javascript code.
  • 更多推荐

    页内锚链接的htaccess异常

    本文发布于:2023-11-01 11:18:31,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1549193.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:锚链   异常   htaccess

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!