如何使 rewriterule 添加锚点到 wordpress url 而不是“/”?

发布于 2024-12-25 14:05:45 字数 770 浏览 4 评论 0原文

我是 .htaccess 新手,但我认为这是最好的方法。 我需要让 wordpress 将 url 中的“/”更改为“#”。结构如下: http://thewebsite/pageid/sub_pageid ,所需的效果将是 http://thewebsite/pagid#sub_pageid 。我尝试了一些在网上找到的规则,但每一条都失败了。我相信它是因为我应用了这些规则。我只有一个页面有子页面,并且它是唯一需要重写的页面。重定向后我希望 jquery 向下滚动到锚点。

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /thewebsite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /thewebsite/index.php 
</IfModule>
# END WordPress

我尝试过 RewriteRule ^page/ page# [NE] 但失败了。我尝试了很多其他方法,但没有效果。希望有人以前遇到过这个问题并且知道,或者可以解释我失败的原因。 谢谢!

I'm a .htaccess nOOb, but I think this is the best way to do it.
I need to make wordpress change the '/' into '#' in the url. The structure goes like:
http://thewebsite/pageid/sub_pageid and the desired effect would be http://thewebsite/pagid#sub_pageid . I've tried few rules I found on-line, but each one of them have failed. I believe its because I have those rules applied to it. There is only one page I have subpages for and it's the only one that needs rewriting. After the redirect I want jquery to scroll down to the anchor.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /thewebsite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /thewebsite/index.php 
</IfModule>
# END WordPress

I've tried RewriteRule ^page/ page# [NE] but I've failed. I've tried many others, but no effect. Hope someone had this problem before and just knows, or can explain why I fail.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲸落 2025-01-01 14:05:45

如果您只是尝试为单个页面重写它,请尝试:

RewriteRule [page\_id]\/(.*) /page_id#$1 [NE,R=301]

这应该是特定页面 ID 或字符串所需的全部内容。 NE 表示“noescape”,允许您重写井号 (#) 符号。

对于任何页面 ID:

RewriteRule ([0-9]+)\/(.*) /$2#$1 [NE,R=301]

If you are just trying to rewrite it for a single page, try:

RewriteRule [page\_id]\/(.*) /page_id#$1 [NE,R=301]

That should be all you need for a specific page id or string. NE means 'noescape,' allowing you to rewrite the hash (#) symbol.

For any page id:

RewriteRule ([0-9]+)\/(.*) /$2#$1 [NE,R=301]
寂寞花火° 2025-01-01 14:05:45

如果不需要,请尝试以下代码,跳过重写条件 download/.php ,确保您必须将 404 文件放入您的域文件夹中

 RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule (.*) index.php
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteCond %{SCRIPT_FILENAME} .*\.php|/$
    RewriteCond %{SCRIPT_FILENAME} !download\.php
    RewriteRule !/ index.php

    ErrorDocument 401 /401.html

希望它对您有帮助

Try this following code if not necessary skip the rewrite contidion download/.php ,make sure that you have to put a 404 file in your domain folder

 RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule (.*) index.php
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteCond %{SCRIPT_FILENAME} .*\.php|/$
    RewriteCond %{SCRIPT_FILENAME} !download\.php
    RewriteRule !/ index.php

    ErrorDocument 401 /401.html

Hope it helps you

剧终人散尽 2025-01-01 14:05:45

好的,
因为我是 .htaccess-lame,所以我决定使用 jQuery 来解决我的问题。我使用 .attr() 来替换原始属性“href”。我已经设法更改子页面的链接:

$(function(){
        $('#menu_on_HOME a').each(function(i){
        var atitle = $(this).attr("href");
        var strRep = atitle;
            n = strRep.lastIndexOf('/'); //finds the last occurence of '/' to replace with '#'
                if (n >= 0 && n + strRep.length >= strRep.length) {
                    strRep = strRep.substring(0, n) + "#" + strRep.substring(n + 1, n + strRep.length); // this is where magic happens
                }
        $(this).attr("href", strRep); //here is where I replace the original attribute 'href'
        });
    });

上面的函数将我发送到 page#subpage,其中 PAGE 包含所有子页面的内容,单击 menu_on_HOME 链接会直接转到子页面锚点。此外,在实际的页面上,我有子页面的内部链接(内容可以通过cms作为页面进行编辑),但我不需要深入子页面,而是获得所需的锚点,所以我使用了 ScrollTo

 $(function(){
        $('menu_on_PAGE a').each(function(i){
        var atitle = $(this).attr("href");
        var strRep = atitle;
            n = strRep.lastIndexOf('/'); 
            var name;
                if (n >= 0 && n + strRep.length >= strRep.length) {
                    name = strRep.substring(n + 1, n + strRep.length);
                    strRep = strRep.substring(0, n) + "#" + name;
                $(this).click(function(event){
                    var scroller = '#content a[name=' + name + ' ]'; //point to the anchor
            $(window).scrollTo(scroller,{duration:1500});
            });
                }
        $(this).attr("href", strRep);

        });
    });
$(window)._scrollable();     

我不知道如何解决方案是“对”或“错”,但它对我来说完美无缺:)

Ok,
Since I'm a .htaccess-lame I decided to use jQuery to solve my problem. I used .attr() to replace the original attribute 'href'. I've managed to change the links to subpages with:

$(function(){
        $('#menu_on_HOME a').each(function(i){
        var atitle = $(this).attr("href");
        var strRep = atitle;
            n = strRep.lastIndexOf('/'); //finds the last occurence of '/' to replace with '#'
                if (n >= 0 && n + strRep.length >= strRep.length) {
                    strRep = strRep.substring(0, n) + "#" + strRep.substring(n + 1, n + strRep.length); // this is where magic happens
                }
        $(this).attr("href", strRep); //here is where I replace the original attribute 'href'
        });
    });

The function above sends me to page#subpage where PAGE contains the content of all subpages and clicking on the menu_on_HOME link goes straight to subpage anchor. Moreover, on the actual PAGE i have internal links to subpages (the content can be editable via cms as a page) but I didn't need to go deeper into subpage but rather get the desired anchor, so I used ScrollTo :

 $(function(){
        $('menu_on_PAGE a').each(function(i){
        var atitle = $(this).attr("href");
        var strRep = atitle;
            n = strRep.lastIndexOf('/'); 
            var name;
                if (n >= 0 && n + strRep.length >= strRep.length) {
                    name = strRep.substring(n + 1, n + strRep.length);
                    strRep = strRep.substring(0, n) + "#" + name;
                $(this).click(function(event){
                    var scroller = '#content a[name=' + name + ' ]'; //point to the anchor
            $(window).scrollTo(scroller,{duration:1500});
            });
                }
        $(this).attr("href", strRep);

        });
    });
$(window)._scrollable();     

I don't know how 'right' or 'wrong' the solution is, however it works for me flawlessly :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文