页内锚链接的htaccess异常 [英] htaccess exception for in-page anchor links

查看:115
本文介绍了页内锚链接的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:

https://www.mywebsite.com/privacy-policy#information-we-collect

系统输出以下内容:

https://www.mywebsite.com/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:

https://www.mywebsite.com/privacy-policy#information-we-collect
https://www.mywebsite.com/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:

https://www.mywebsite.com/privacy-policy/

通常它会调整为:

https://www.mywebsite.com/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:

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

但这完全没有响应:

https://www.mywebsite.com/terms-of-service#limitation-of-liability

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

It only works within the Terms of Service page.

解决方案

将我的评论转换为答案。

Converting my comments to an answer.

此异常无效:


  1. 因为Web服务器(或重写规则)对锚点一无所知,因为URL的一部分开头 完全在浏览器中处理,不会发送到Web服务器。

  2. #information-we-collect 部分不会发送到Web服务器,因此为创建一个例外#... 不能真正解决问题。必须有其他方法来创建异常,例如添加前缀/后缀或伪查询参数,重写规则可以查看并对其执行操作。

  3. 或者,您可以在客户端本身(即在Javascript代码内部)进行处理。

  1. 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.
  2. #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.
  3. Alternatively you can handle it on client side itself i.e. inside Javascript code.

这篇关于页内锚链接的htaccess异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆