【问题标题】:Apache rewrite to map # based URL into proper URLsApache 重写以将基于 # 的 URL 映射到正确的 URL
【发布时间】:2018-07-21 05:34:29
【问题描述】:

长版(您可以跳到 TL;如果您愿意,可以跳到 DR):

我正在使用由其他人设置的 Wordpress 网站。该网站有多个页面,其中页面具有选项卡式内容,可通过 # 访问。例如:

www.example.com/services/category1/#tab-service1
www.example.com/services/category1/#tab-service2
www.example.com/services/category2/#tab-service1
www.example.com/services/category2/#tab-service2
www.example.com/services/category2/#tab-service3

现在,当搜索引擎索引时,它们只会索引www.example.com/services/category1/www.example.com/services/category2/。这就产生了一个问题,我们不能让搜索引擎直接指向给定选项卡中的内容。我们想要的是让搜索引擎显示将用户直接带到(例如)www.example.com/services/category2/#tab-service3 的链接。

现在,我认为 google 无法自行索引此类基于 # 的内容。所以,我正在考虑使用 apache rewrites 来尝试解决这个问题。我只能访问 .htaccess 文件(从配置的角度来看)。

TL;DR

如何使用 apache 重定向将 www.example.com/services/category1/service3/ 重定向到 www.example.com/services/category1/#tab-service3(我可以访问 .htaccess 文件)?

这是我正在尝试的,但它不起作用:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  ^/services/category1/([a-z0-9])/? [NC]
RewriteRule .*       /services/category1#tab-%1           [R,NE,L]

有人还建议查看 pushState 服务器配置以解决此问题。我不确定如何使用 pushState。

更新:

我已将重写更新为以下内容,但仍然无法正常工作。它一直显示 Wordpress 的 404 页面

<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /domainfolder/
RewriteCond %{REQUEST_URI}  ^/services/category1/([a-z0-9]+)/?$ [NC]
RewriteRule ^/services/category1/([a-z0-9]+)/?$ /services/category1/#$1 [NE,R,L]
</IfModule>

【问题讨论】:

    标签: wordpress apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您的%{REQUEST_URI} 正则表达式错误。模式 ^/services/ category1/([a-z0- 9 ])/? 匹配 /services/category1/{any 1 char of a-z or 0-9} 格式,后跟可选的斜杠。所以这与您的请求不匹配 /services/category1/service3 但匹配 /services/category1/a/

    你应该使用

    ^/services/category1/([a-z0-9]+)/?$
    

    【讨论】:

    • 好的。我已经做到了。仍然不起作用:(。请参阅我原来的问题中的更新。
    • 您需要从规则模式中删除前导斜杠。将其更改为RewriteRule ^services/category1/([a-z0-9]+)/?$ /services/category1/#$1 [NE,R,L]
    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2011-12-25
    • 2015-05-22
    • 1970-01-01
    • 2012-11-23
    • 2018-08-12
    相关资源
    最近更新 更多